Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Date.toString() returning the wrong time

I have this date that is saved in my database as UTC and looks 2019-04-25T08:47:14.813 when I console log it out.

I read to convert that to my local time I can just cast it as a Date and the do toString() and it would be my local time zone, but it doesn't seem to work with UK Daytime saving.

For example, if I take the string above and do this:

let d = new Date(item.lastChecked);
console.log(d.toString());
console.log(item.lastChecked);

The response I get is:

Thu Apr 25 2019 08:47:14 GMT+0100 (British Summer Time)

2019-04-25T08:47:14.813

I don't want it to save (British Summer Time). I want it to say 9:47 instead. How can I do that?

like image 394
r3plica Avatar asked Sep 26 '26 01:09

r3plica


1 Answers

Add 'Z' (which indicates that date is in UTC) to your input string

let d=new Date('2019-04-25T08:47:14.813'+'Z');

console.log(d.toString());
like image 112
Kamil Kiełczewski Avatar answered Sep 28 '26 14:09

Kamil Kiełczewski



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!