moment / moment-timezone

Timezone support for moment.js
momentjs.com/timezone
MIT License
3.83k stars 838 forks source link

tz is current date dependent #1030

Closed achifal closed 1 year ago

achifal commented 1 year ago

Moment-timezone version which you use:

Version: 0.5.39

Issue description:

console.log(moment.tz('05-1', 'MM-d', true, 'UTC'));
// returns Invalid date in 2022
// returns 2023-05-01T00:00:00.000Z from January 1st, 2023
// returns Invalid date in 2024
gilmoreorless commented 1 year ago

Yes, parsing a partial date will fill in the gaps with the current year and month. However, the reason you're getting invalid dates sometimes is because the token d represents a day of the week, not a day of the month (which is D). I'm not sure if this was the intention.

So 05-1 is being parsed as a Monday (day-of-week 1) in the 5th month. Without a day of the month present, the parser defaults to the 1st. May 1st, 2023 happens to be a Monday, which is why that example works out. But the documentation mentions:

The parser also confirms that the day-of-week (when included) is consistent with the date.

Since May 1st, 2022 and May 1st, 2024 are not Mondays, the parser fails the validation.