Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getDay doesn't let you set weekStartsOn #1287

Closed
laurentsenta opened this issue Aug 11, 2019 · 5 comments
Closed

getDay doesn't let you set weekStartsOn #1287

laurentsenta opened this issue Aug 11, 2019 · 5 comments

Comments

@laurentsenta
Copy link

Hi there, thanks for maintaining this super useful library,
I have a remark / request / issue regarding getDay:

getWeekOfMonth accept options such as locale, weekStartsOn.
https://date-fns.org/v2.0.0-beta.4/docs/getWeekOfMonth

This means that:

const aug11_2019 = new Date(2019, 7, 11)
getWeekOfMonth(aug11_2019) === 3 // Sunday, third week of august
getWeekOfMonth(aug11_2019, {weeksStartsOn: 1) === 2 // Week starts on Monday, we're still on week 2

(supposedly, there's a known bug with Sundays: #1040)

I expect the getDay function to let me set locale, weeksStartsOn too, so that:

getDay(aug11_2019) === 0 // Sunday, first day of the week
getDay(aug11_2019, {weeksStartsOn: 1) === 6 // last day of the previous week
@leshakoss
Copy link
Contributor

leshakoss commented Aug 12, 2019

@lsenta hi. The behaviour of getDay is correct according to the current design of date-fns functions.

0 always means Sunday, and 6 always means Saturday regardless of weekStartsOn. That's how the rest of the functions work:

> const aug12th = new Date(2019, 7 /* Aug */, 12)

> const SUNDAY = 0
> const MONDAY = 1

> setDay(aug12th, SUNDAY).toString()
'Sun Aug 11 2019 00:00:00 GMT+0200 (Central European Summer Time)'
> // 0 is Sunday, the week starts on Sunday and ends on Saturday

> setDay(aug12th, SUNDAY, { weekStartsOn: MONDAY }).toString()
'Sun Aug 18 2019 00:00:00 GMT+0200 (Central European Summer Time)'
> // 0 is still Sunday, but the borders of the week moved to Monday-Sunday

@hernansartorio
Copy link

The behaviour of getDay is correct according to the current design of date-fns functions.
0 always means Sunday, and 6 always means Saturday regardless of weekStartsOn. That's how the rest of the functions work:

@leshakoss can you explain the reasoning behind that design?

startOfWeek and endOfWeek, for example, also allow a locale object. If I'm working with a locale where the week starts on Monday, it would make a lot of sense that getDay also accepts a locale and returns the day according to that locale.

@kossnocorp
Copy link
Member

kossnocorp commented Dec 19, 2019

@hernansartorio "please" would be helpful.

First of all, day (just like a month) is an index number, so it should be static and always point on the same entity. Week, year, and quarter, unlike day and month, are relative to the used calendar system. For example, 2005-01-01 might give you 2004 year if you ISO week numbering calendar but 0 always points to Sunday or January (at least in JavaScript).

Secondly, it doesn't make sense from the API point of view:

const date = new Date(2019, 11, 19)

startOfWeek(date, { weekStartsOn: 1 })
//=> 2019-12-16

getDay(date, { weekStartsOn: 5 })
//=> what do you expect to get?

I think you might confuse the day of the week with the day number relative to the start of the week. It's not the same, and we don't use the latter anywhere. It's also not common in the real world. You say, "let's meet on Tuesday," but never "let's meet on the third day of the week." At first, it's not intuitive but also could lead to confusion when people from different parts of the world try to arrange a meeting.

@hernansartorio
Copy link

@kossnocorp sorry, I was definitely missing a “please” there. I didn’t mean to sound demanding at all.

Thanks for the explanation, that makes sense. Like you say, I was confusing getting the day of the week with getting the day number relative to the start of the week (which I ended up solving with differenceInCalendarDays(date, startOfWeek(date, { locale })).

Anyways, thank you for creating and maintaining this library. It's incredibly well-done and a life-saver when working with dates.

@kossnocorp
Copy link
Member

@hernansartorio no worries, no grudge at all. I'm glad that it was helpful, and thank you so much for the kind words.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants