r3plica
r3plica

Reputation: 13367

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?

Upvotes: 3

Views: 250

Answers (1)

Kamil Kiełczewski
Kamil Kiełczewski

Reputation: 92367

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());

Upvotes: 3

Related Questions