2

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?

Nour May
  • 21
  • 1
  • 5

2 Answers2

1

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.

Hassan Niazi
  • 330
  • 2
  • 3
  • 13
-1
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"

Simon
  • 369
  • 1
  • 9