-2

This is the Function that Determined the current Date and Time

var min = new Date().getMinutes(); //To get the Current Minutes
var hours = new Date().getHours(); //To get the Current Hours

var date = new Date().getDate(); //To get the Current Date
var month = new Date().getMonth() + 1; //To get the Current Month
var year = new Date().getFullYear(); //To get the Current Year

This is where the Const with the If Statement i

const monthInLetters = (month) => {


if (month== '3'){ return ('January')}

  
};

And then it gets rendered in this Text box

<Text style={{color: 'white' fontSize: 40}}>{date}, {month} {monthInLetters}, {year}</Text>

I want it to check the month of the year in numbers and then convert that to the month as a string? How would I do that?

jmoerdyk
  • 5,544
  • 7
  • 38
  • 49
Josef Pa
  • 39
  • 1
  • 8
  • I tried it and it does not seem to work? – Josef Pa Mar 06 '22 at 13:42
  • It looks like you're trying to render the function itself, rather than invoke the function and pass it a `month` value. Did you just forget to invoke the function? Perhaps you could be more specific about how this doesn't work? – David Mar 06 '22 at 13:48
  • Sorry I am new to React Native, What do you mean by passing month? – Josef Pa Mar 06 '22 at 13:52
  • 1
    It's not specific to React Native. It's a fundamental part of JavaScript (and almost every other popular programming language). `monthInLetters` is a function. It expects one parameter. Where do you call that function and pass it that parameter? – David Mar 06 '22 at 13:55
  • Please do not edit solution announcements into the question or title. See [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – jmoerdyk Oct 24 '22 at 23:22

2 Answers2

1

You don't need to "check the month of the year in numbers and then convert that to the month as a string" - you can just use toLocaleString, e.g.

const date = new Date();
const month = date.toLocaleString('default', { month: 'long' });
console.log(month);

Also there is no need to create a new date object for each part of the date. e.g.

var date = new Date()

var min = date.getMinutes(); //To get the Current Minutes
var hours = date.getHours(); //To get the Current Hours
var date = date.getDate(); //To get the Current Date
var month = date.getMonth() + 1; //To get the Current Month
var year = date.getFullYear(); //To get the Current Year
Fraser
  • 15,275
  • 8
  • 53
  • 104
1

Answering as you said it doesn't work

const monthInLetters = (month) => {
  let months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
      return months[month - 1];
};
Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47