-
I have node 14.17.0 I parse a new date object from a string that says 24.9.1960 in various formats. What can I do to fix this?
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The input is treated as being in the system's local time zone, not as UTC. When you If you really meant that input to be UTC, you could use the // Note: These are outputs from an eastern time zone system
// same as: new Date(1960, 8, 24)
let d = parse('1960-09-24', 'yyyy-MM-dd', new Date());
console.log(d.toString()); // Sat Sep 24 1960 00:00:00 GMT-0400 (Eastern Daylight Time)
console.log(d.toISOString()); // 1960-09-24T04:00:00.000Z
// same as: new Date(Date.UTC(1960, 8, 24))
d = parse('1960-09-24Z', 'yyyy-MM-ddX', new Date());
console.log(d.toString()); // Fri Sep 23 1960 20:00:00 GMT-0400 (Eastern Daylight Time)
console.log(d.toISOString()); // 1960-09-24T00:00:00.000Z It's worth mentioning that This may also be useful: https://date-fns.org/docs/Time-Zones |
Beta Was this translation helpful? Give feedback.
The input is treated as being in the system's local time zone, not as UTC.
When you
console.log
a Date object in Node, it stringifies it using.toISOString()
which is a UTC interpretation of the date. So, if you are in a time zone with a positive offset related to UTC, any midnight local date will be represented as the previous day in UTC.If you really meant that input to be UTC, you could use the
X
token and add aZ
in the date string.