I am going crazy with this Date to String conversion into a TypeScript project:
I have this function performing my conversion:
fromDateToString(date: Date) : String {
let dateAsString = date.toISOString().substr(0, 19)
console.log(dateAsString);
return dateAsString;
}
As you can see it take a Date object containing the following value:
Wed Feb 08 2017 07:00:00 GMT-0800 (Ora standard del Pacifico USA)
and, in theory it have to convert this date in the following string format:
2017-02-08T07:00:00"
But I am finding a strange behavior on the time !!! The generated string is not the previous one but it is:
2017-02-08T15:00:00"
The generated time section is not 07:00:00 but it is 15:00:00
Why? What is wrong? What am I missing? How can I fix this issue?
I am going crazy with this Date to String conversion into a TypeScript project:
I have this function performing my conversion:
fromDateToString(date: Date) : String {
let dateAsString = date.toISOString().substr(0, 19)
console.log(dateAsString);
return dateAsString;
}
As you can see it take a Date object containing the following value:
Wed Feb 08 2017 07:00:00 GMT-0800 (Ora standard del Pacifico USA)
and, in theory it have to convert this date in the following string format:
2017-02-08T07:00:00"
But I am finding a strange behavior on the time !!! The generated string is not the previous one but it is:
2017-02-08T15:00:00"
The generated time section is not 07:00:00 but it is 15:00:00
Why? What is wrong? What am I missing? How can I fix this issue?
Share Improve this question edited Jul 3, 2020 at 11:21 takendarkk 3,4428 gold badges27 silver badges38 bronze badges asked Jul 3, 2020 at 11:19 AndreaNobiliAndreaNobili 43.1k122 gold badges361 silver badges679 bronze badges 3-
2
Look at the full string
toISOString
is giving you. – T.J. Crowder Commented Jul 3, 2020 at 11:21 -
Because of timezones.
toISOString
will always return the time at UTC timezone. – Pedro Lima Commented Jul 3, 2020 at 11:27 - As per the docs, toISOString() always returns the date in UTC+0 timezone, so you have to search for a different conversion method. – Shilly Commented Jul 3, 2020 at 11:27
3 Answers
Reset to default 6The value isn't wrong; you're disregarding the timezone. toISOString
returns a string written in UTC with Z
at the end saying it's in UTC. You're stripping off the part of the string saying what timezone it's in.
If you like, you can get that string in local time instead by subtracting the timezone offset:
function fromDateToString(date/*: Date*/)/* : String*/ {
date = new Date(+date);
date.setTime(date.getTime() - (date.getTimezoneOffset() * 60000));
let dateAsString = date.toISOString().substr(0, 19);
return dateAsString;
}
console.log(fromDateToString(new Date()));
Just beware that your code using that string handles it as a local time, not UTC.
2017-02-08T15:00:00
-> this is UTC date (ISO) [UTC has 0:00 offset]
2017-02-08T07:00:00
-> this is GMT-0800
so if we convert 2017-02-08T07:00:00
to UTC we need to add 8:00 hours so 7 + 8 = 15
so its giving 2017-02-08T15:00:00
You directly cannot ignore timezone offset without adding/subtracting.
That happens because the EcmaScript algorithm for generating an ISO string always generates it with the UTC+0 timezone. You don't need (and shouldn't) modify the Date
object. Here's an algorithm to get a ISO string with the local timezone:
let toLocalISOString;
{
const pad = function pad(str) {
return String(str).padStart(2, "0");
}
toLocalISOString = function toLocalISOString(date) {
const tzOffset = date.getTimezoneOffset(),
tzPlus = tzOffset >= 0;
return date.getFullYear() +
'-' + pad(date.getMonth() + 1) +
'-' + pad(date.getDate()) +
'T' + pad(date.getHours()) +
':' + pad(date.getMinutes()) +
':' + pad(date.getSeconds()) +
'.' + (date.getMilliseconds() / 1000).toFixed(3).slice(2, 5) +
(tzPlus ? "+" + pad(tzOffset / 60) : "-" + pad((tzOffset / 60).toString().slice(1))) +
":" + pad((tzOffset % 60).toFixed(0));
};
}