parse vs new Date #2759
Unanswered
alex-enchi
asked this question in
Q&A
Replies: 1 comment
-
When you pass a string to the Date JS constructor, it interprets it as being in UTC. If you omit any time zone information in the import { parse, parseISO } from "date-fns";
const str = "2018-11-13";
const strUTC = str + "Z";
// input interpreted as local time zone
// Tue Nov 13 2018 00:00:00 GMT-0500 (Eastern Standard Time)
console.log(new Date(2018, 10, 13));
console.log(parse(str, "yyyy-MM-dd", new Date()));
console.log(parseISO(str));
// input interpreted as UTC
// Mon Nov 12 2018 19:00:00 GMT-0500 (Eastern Standard Time)
console.log(new Date(Date.UTC(2018, 10, 13)));
console.log(new Date(str));
console.log(parse(strUTC, "yyyy-MM-ddX", new Date()));
console.log(parseISO(strUTC)); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Greetings
I recently tried to debug one of the datepickers and found interesting difference between how native
Date
parses date and howdate-fns
is doing this.It can be checked in that sandbox
date-fns
behaviour looks logical (i.e. midnight in the local timezone) but is there a good way to revert or adjust that behaviour to match native Date?Beta Was this translation helpful? Give feedback.
All reactions