'Luxon substracting one day if JS date is UTC
I create a date with Luxon in the following way:
const date = DateTime.utc(2000, 6, 23).toFormat('yyyy-MM-dd')
Then I try to convert it to a JS date by doing this:
const jsDate = new Date(date)
Finally, I convert it back to the 'yyyy-MM-dd' format with
const parsedDate = DateTime.fromJSDate(jsDate).toFormat('yyyy-MM-dd')
But instead of giving me 2000-06-23 it gives me 2000-06-22. Is this a bug on Luxon or I need to do something to get the correct date?
Solution 1:[1]
I try to convert it to a JS date by doing
const jsDate = new Date(date)
Uh, why not just date.toJSDate()?
Instead of giving me 2000-06-23,
DateTime.fromJSDate(jsDate).toFormat('yyyy-MM-dd')gives me 2000-06-22. Is this a bug on Luxon or I need to do something to get the correct date?
According to its docs, fromJSDate defaults to creating a DateTime instance in the local (system) timezone. If you expect another UTC date back, you need to specify that:
DateTime.fromJSDate(jsDate, {zone: 'utc'}).toFormat('yyyy-MM-dd')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bergi |
