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

transform timestamp to mm/dd/yyyy

I have a date value in my React app that’s returned from MySQL as a string in this format:

"2012-03-04T00:00:00.000+00:00"

The date gets transformed, using moment, to this format:

03/04/2012

Using moment, this is simple:

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

moment(myDate).format('MM/DD/YYYY')

But I’d like to change this, since moment is no longer maintained.

Is there a simple way to do this transformation with some built-in javascript date function?

>Solution :

You can use this:

const date = new Date("2019-08-01T00:00:00.000+00:00")
const year = date.getFullYear().toString().padStart(4, '0')
const month = (date.getMonth() + 1).toString().padStart(2, '0')
const day = date.getDate().toString().padStart(2, '0')
const formatted = `${month}/${day}/${year}`
console.log(formatted)

But I would just another library like date-fns or dayjs

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