date-fns / utc

date-fns UTC utils
MIT License
85 stars 9 forks source link

UTCDate does not work #1

Closed illuminati-confirmed closed 12 months ago

illuminati-confirmed commented 12 months ago

I am trying to use date-fns to parse a string as if it were a UTC date instead of a local date, yet parsing does not return a UTC date and instead only returns a local date, but minus the timezone offset to pretend it is from UTC.

parse(date, 'yyyy-MM-dd', new UTCDate()); // returns 2023-05-02T14:00:00.000Z for input 2023-05-03 As a unix timestamp this is 1683036000 which corresponds to 2023-05-02T14:00:00 which is clearly NOT 2023-05-03 as UTC time.

kossnocorp commented 12 months ago

Hey! The third argument is not the constructor to use but just the backup date to use when it's not enough information in the input to build a date.

date-fns is built, so the date is always in the local timezone, hence the result.

You actually don't need date-fns for that as Date always parse date strings without timezone in UTC:

image

But accidentally, you're right. UTCDate indeed had a problem accepting strings, which I fixed in v1.1.0:

image