1
votes

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?

2
It's not subtracting a day. Midnight GMT+1 is 11pm GMT. - jonrsharpe
timezones.jpg same issue as there: stackoverflow.com/questions/50473932/… - GottZ

2 Answers

1
votes

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.

-1
votes
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"