Const that returns 一个值取决于另一个值

Const that returns a value depending on another value

这是确定当前日期和时间的函数

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

这是带有 If 语句的 Const 所在的位置

const monthInLetters = (month) => {


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

  
};

然后它会呈现在这个文本框中

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

我想让它以数字形式检查一年中的月份,然后将其转换为字符串形式的月份? 我该怎么做?

您不需要“检查数字中的月份,然后将其转换为字符串形式的月份”- 您可以只使用 toLocaleString,例如

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

也不需要为日期的每个部分创建一个新的日期对象。例如

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

按你说的回答不行

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