Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript wrong timestamp formatting [duplicate]

I'm currently trying to format a timestamp in JavaScript. Sadly, the result is a bit strange. This is the date I'm trying to format:

Thu May 23 2019 17:34:18 GMT+0200 (Mitteleuropäische Sommerzeit)

And this is how it should looks like after formatting it:

23.05.2019

This is my code:

j = myDateString;
console.log(j.getUTCDate() + "." + j.getUTCMonth() + "." + j.getUTCFullYear());

But when I run my code, this is the result:

23.4.2019

like image 703
Mr. Jo Avatar asked Dec 22 '25 00:12

Mr. Jo


1 Answers

getUTCMonth() starts from 0 (January), fix it by adding one.

console.log(j.getUTCDate() + "." + (j.getUTCMonth() + 1) + "." + j.getUTCFullYear());

like image 59
devgianlu Avatar answered Dec 24 '25 13:12

devgianlu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!