Javascript Format Date without Moment.js (Eg: 17 July, 2021 )

Almost everyone like me might be using the plugin moment.js to format a date-time-stamp to a readable date or time - Eg: 14 July 2021 or 11:58 pm

Fortunately, there is a native javascript API to format date and time.

Demo and Example to format date:

With the help of toLocaleDateString we can format a date-time-stamp to a readable language sensitive representation.

const date = new Date();

Sat Jul 17 2021 19:04:31 GMT+0530 (India Standard Time)

date.toLocaleDateString("en-IN", {
"year": "numeric",
"month": "long",
"day": "numeric"
})

"17 July 2021"

(Formatted Date)

Demo and Example to format time:

With the help of toLocaleTimeString we can format a date-time-stamp to a readable language sensitive representation.

const date = new Date();

Sat Jul 17 2021 19:04:31 GMT+0530 (India Standard Time)

date.toLocaleTimeString("en-IN", {"hour": "numeric"})

"7 pm"

(Formatted Time)

If you liked my content you can follow me on twitter for more such content-

8