marnusw / date-fns-tz

Complementary library for date-fns v2 adding IANA time zone support
MIT License
1.06k stars 116 forks source link

When input not ISO8601 time string like 2023/11/11 to toDate will return wrong date #257

Open a631953720 opened 10 months ago

a631953720 commented 10 months ago

Hi there,

I really appreciate this library for handling dates across different time zones.

Thans to the date-fns-tz developers for providing such a useful library.

I'm confused about the toDate function. When I input a string like 2023/11/11, which is not an ISO8601 time string, toDate returns 2022-12-31T16:00:00.000Z date.

I noticed that the datePattern in the splitDateString function returns a matched result as shown below:

const datePattern = new RegExp(/^([0-9W+-]+)(.*)/);

datePattern.exec('2023/11/11'); // [2023/11/11, 2023, /11/11, 0, 2023/11/11, undefined]

As a result, dateStrings.date only captures the year (2023), leading to date.setUTCFullYear(2023).

Is there any plan to either restrict or support this specific time string format?

This is my codeSendBox to present these different case result.

https://codesandbox.io/s/date-fns-tz-todate-4mql3c?file=/src/index.mjs

Thanks