Woodchuck
Woodchuck

Reputation: 4414

Need to transform timestamp to mm/dd/yyyy format

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:

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?

The answers here and here don't help here, as they include no details on formatting the resulting date the way I need it.

Upvotes: 0

Views: 116

Answers (1)

Konrad
Konrad

Reputation: 24661

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

Upvotes: 1

Related Questions