how to get this specific datetime format in javascript

1254 views javascript
2

In javascript, is there a way to convert a date time to the following format:

// 11/3/18, 12:00 AM

Date().toString() gives me:

Sat Nov 03 2018 00:00:00 GMT+0000 (UTC)

Thanks.

answered question

Read about monent.js, I think that library will help you a lot.

1 Answer

1

This is an alternative to format dates, the function Date.prototype.toLocaleDateString allows you to format date according to options/flags.

Some js engines manage the format process differently (so, this is implementation dependent), therefore be careful. Further, you need to check for compatibility in browsers.

let today = new Date();
var options = { year: 'numeric', month: 'numeric', day: 'numeric', hour12: true, hour: 'numeric', minute: 'numeric' };

console.log(today.toLocaleDateString('en-US', options));

posted this

Have an answer?

JD

Please login first before posting an answer.