DEV Community

Handling Dates in JavaScript

John Au-Yeung on January 11, 2020

Subscribe to my email list now at http://jauyeung.net/subscribe/ Follow me on Twitter at https://twitter.com/AuMayeung Many more articles at http...
Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt
  1. What is the correct way to write 2020-01-01 1:00, so that Date constructor / moment doesn't complain (without directly specifying Date format)?
  2. moment.js is large and mutable. One mighy consider alternatives, such as Luxon, date-fns or dayjs.
Collapse
 
aumayeung profile image
John Au-Yeung
  1. This is the closest thing I can find to format dates in the format you ask for gist.github.com/noromanba/6737784

  2. I think date-fns is also good.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

Open your console and type Intl you might find some very useful date formatting power in this API as well as a bunch of other good stuff.

Collapse
 
aumayeung profile image
John Au-Yeung

There's actually a lot of features for internationalization un JavaScript, like sorting strings in a locale-sensitive manner, formatting numbers, plurals, lists, and showing relative time.

Dates are just the beginning.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

I like the currency section of this API, fantasticly under utilized though 😅

Thread Thread
 
aumayeung profile image
John Au-Yeung

Yea. I only discovered it on MDN when I'm writing about it.

Collapse
 
osde8info profile image
Clive Da

you might also like to have a look at the JS date and time formatting functions

new Date().toLocaleDateString("en-GB", options);
new Date().toLocaleTimeString("en-GB")

Collapse
 
aumayeung profile image
John Au-Yeung

toLocaleDateString actually has lots of options to format dates.

It's also sensitive to locale, which is handy for formatting dates in any format in the world almost.