Format Date with native methods in Javascript
Posted on August 14, 2019
Another little jam learned today.
I have to format dates and for that task I've usually relied to moment
library.
This time I wanted to try a different route exploring some built-in methods.
Nowadays, you can use the .toLocaleDateString
method of the Date
object to performe some basic formatting.
Here how it looks like:
let date = new Date().toLocaleDateString('it-IT', { day: '2-digit', month: '2-digit', year: 'numeric' })
There's the locale parameter and an optional configuration which is well documented here.
For basic formatting the above options are good enough.