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 for…


This content originally appeared on DEV Community and was authored by Jyotishman Saikia

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-

https://twitter.com/frontend_jsx


This content originally appeared on DEV Community and was authored by Jyotishman Saikia


Print Share Comment Cite Upload Translate Updates
APA

Jyotishman Saikia | Sciencx (2021-07-17T13:42:44+00:00) Javascript Format Date without Moment.js (Eg: 17 July, 2021 ). Retrieved from https://www.scien.cx/2021/07/17/javascript-format-date-without-moment-js-eg-17-july-2021/

MLA
" » Javascript Format Date without Moment.js (Eg: 17 July, 2021 )." Jyotishman Saikia | Sciencx - Saturday July 17, 2021, https://www.scien.cx/2021/07/17/javascript-format-date-without-moment-js-eg-17-july-2021/
HARVARD
Jyotishman Saikia | Sciencx Saturday July 17, 2021 » Javascript Format Date without Moment.js (Eg: 17 July, 2021 )., viewed ,<https://www.scien.cx/2021/07/17/javascript-format-date-without-moment-js-eg-17-july-2021/>
VANCOUVER
Jyotishman Saikia | Sciencx - » Javascript Format Date without Moment.js (Eg: 17 July, 2021 ). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/17/javascript-format-date-without-moment-js-eg-17-july-2021/
CHICAGO
" » Javascript Format Date without Moment.js (Eg: 17 July, 2021 )." Jyotishman Saikia | Sciencx - Accessed . https://www.scien.cx/2021/07/17/javascript-format-date-without-moment-js-eg-17-july-2021/
IEEE
" » Javascript Format Date without Moment.js (Eg: 17 July, 2021 )." Jyotishman Saikia | Sciencx [Online]. Available: https://www.scien.cx/2021/07/17/javascript-format-date-without-moment-js-eg-17-july-2021/. [Accessed: ]
rf:citation
» Javascript Format Date without Moment.js (Eg: 17 July, 2021 ) | Jyotishman Saikia | Sciencx | https://www.scien.cx/2021/07/17/javascript-format-date-without-moment-js-eg-17-july-2021/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.