moment / luxon

⏱ A library for working with dates and times in JS
https://moment.github.io/luxon
MIT License
15.12k stars 728 forks source link

DateTime.hasSame(...) doesn't work with months #1502

Closed MSeeligHEC closed 10 months ago

MSeeligHEC commented 10 months ago

Describe the bug Using hasSame to check if two date times have the same month fails.

To Reproduce

const monthMoment = DateTime.fromJSDate(new Date(1971, 8, 8, 0, 0, 0, 0))
monthMoment.hasSame(DateTime.fromJSDate(new Date(2023, 8, 8, 0, 0, 0, 0)), "month") -> returns false

Actual vs Expected behavior Using hasSame to check month should work correctly. If the first date time has september as month and the date time it gets checked with is also in september it shloud return true.

Desktop (please complete the following information):

MSeeligHEC commented 10 months ago

Just updated to 3.4.3 and the Problem still exists.

diesieben07 commented 10 months ago

hasSame('month') asks "do these dates have the same calendar month". September 1971 and September 2023 are not the same calendar month. Even though both are a September, they are in different years and thus are different calendar months. Another way to think about this is that hasSame('month') implies hasSame('year').

If you want to check if the month is the same regardless of year, just check if the month property is the same:

const a = DateTime.fromJSDate(new Date(1971, 8, 8, 0, 0, 0, 0));
const b = DateTime.fromJSDate(new Date(2023, 8, 8, 0, 0, 0, 0));
console.log(a.month === b.month); // true
MSeeligHEC commented 10 months ago

Just to be sure. If i check the day with hasSame it should also return false since they are not in the same year?

diesieben07 commented 10 months ago

Correct. You can think of hasSame(<unit>) checking that unit and all bigger units.