Getting the date in dd/mm/yyyy hh:mm format in JavaScript

Declaring a new date in JavaScript:

jsx
const today = new Date();
// Wed Oct 16 2019 14:05:50 GMT+0700

To get the time in dd/mm/yyyy format, is convenient to use the toLocaleDateString() method:

jsx
const options = { year: 'numeric', month: '2-digit', day: '2-digit' };
const today = (new Date()).toLocaleDateString('es-ES', options);
// 16/10/2019

We could also add the time to the options variable to obtain the date in dd/mm/yyyy hh:mm format:

jsx
const options = { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' };
const today = (new Date()).toLocaleDateString('es-ES', options);
// 16/10/2019 14:14

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.