Esta es la función que determinó la fecha y la hora actuales
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
Aquí es donde la Const con la instrucción If i
const monthInLetters = (month) => { if (month== '3'){ return ('January')} };
Y luego se representa en este cuadro de texto
<Text style={{color: 'white' fontSize: 40}}>{date}, {month} {monthInLetters}, {year}</Text>
¿Quiero que verifique el mes del año en números y luego lo convierta al mes como una cadena? ¿Como podría hacerlo?
No necesita "verificar el mes del año en números y luego convertirlo en el mes como una cadena"; solo puede usar toLocaleString
, por ejemplo
const date = new Date(); const month = date.toLocaleString('default', { month: 'long' }); console.log(month);
Además, no es necesario crear un nuevo objeto de fecha para cada parte de la fecha. p.ej
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
Respondiendo como dijiste no funciona
const monthInLetters = (month) => { let months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; return months[month - 1]; };