Declaring a new date in JavaScript:
jsxconst 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:
jsxconst 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:
jsxconst 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.