Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Const that returns a value depending on another value

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading