Nour May
Nour May

Reputation: 21

toISOString() is subtracting day from value

I have this date format: Tue Oct 20 2020 00:00:00 GMT+0100 (Central European Standard Time)

And when I do :

myValue.toISOString();

that's what I get

2020-10-19T23:00:00.000Z

It's subtracting a day.

How can I solve this without changing date format?

Upvotes: 2

Views: 3130

Answers (2)

Simon
Simon

Reputation: 379

let date = new Date('Tue Oct 20 2020 00:00:00 GMT+0100');
console.log('date: ' + JSON.stringify(date));
let result = date.toLocaleDateString("fr-CA",{year:"numeric", month:"2-digit", day:"2-digit"});
console.log('result: ' + JSON.stringify(result));

date: "2020-10-19T23:00:00.000Z"

result: "2020-10-20"

Upvotes: -1

Hassan Niazi
Hassan Niazi

Reputation: 330

The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ, respectively). The timezone is always zero UTC offset, as denoted by the suffix "Z".

As your current timezone (Central European Standard Time) is GMT+1 Thats why you had the day-1 time. which is infact just UTC current time. Your region is one hour ahead of the UTC. If you check at different time of a day this will not be a difference of one day but just one hour.

Upvotes: 1

Related Questions