-
Notifications
You must be signed in to change notification settings - Fork 729
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
startOf('week') is not respecting locale setting #373
Comments
Correct. Luxon has no sense of local week start conventions and just uses ISO rules. As far as I know, there's no Intl API I can use to get the local first day of the week. |
Can we put this information in the documentation? I scoured it looking for a setting to set what day of the week is considered the start or how luxon decides what the start of the week is and hence what the basis would be for any offset I'd need to control it. |
It is documented in the I suppose we could add that documentation in the |
Yeah, I would have never thought to look at |
Hi would it make sense to have a setter for this? We are running into this issue now where we are grouping data by weeks and we are finding the result is incorrect as our front end respects locale, and our backend is using Luxon and does not. Thanks. |
@gregorskii Local week sounds like a great plugin. It is not something I can reasonably support in the core library, because it would work completely differently from all the other local settings. If this ever moves forward, I will add it to Luxon. |
Would it be possible to have a setter, or is it too integral to how it works internally? |
Meaning a setter for which day of the week is first? No, I don't want to do that. It's not so much about the internals as the API. All the other features work automatically using the locale settings. To have this one other one where the contract is that first you set some setting and then it works, and if you change locales you need to set again, etc, is surprising departure for the API. That's why I'd like it to be a plugin of some kind. |
Makes sense. By plugin do you mean based on the locale? Or does Luxon have the intention of adding plugins? |
I just ran across this limitation too. I implemented my own function to do it in https://gist.github.com/wkeese/20514beb68bc1bac807ec07cda4175db. Note that dojo's data came from the CLDR but we just inlined it into the function. Also, here's the test case I used:
Before reading this ticket, I expected the |
I also have a case where I need to display periods according to the week number (e.g. 2020W27 is 28/6/2020 - 4/7/2020) but consider Sunday as the start of the week, no matter the user's locale. I think we should be able to configure this as a global setting, same as what is considered the first week of the year. Moment.js supports it (https://momentjs.com/docs/#/customization/dow-doy/). |
It's coming soon: https://chromestatus.com/feature/5566859262820352 |
Currently luxon does not support setting first day of the week (moment/luxon#373) therefore indefinitely removing it.
Currently luxon does not support setting first day of the week (moment/luxon#373) therefore indefinitely removing it.
* spike: add luxon based config generator - The luxon mapping was copied from react-component/picker#230 * spike: fix format string difference Luxon behaves different with format strings. ```tsx const l = DateTime.fromFormat("2021.07", "y.MM.dd") // results in an invalid luxon date l.toFormat("y.MM.dd") // Invalid DateTime const m = moment("2021.07", "y.MM.dd"); // results in an valid moment date m.format("y.MM.DD") // "2021.07.01" ``` * spike: add default value It might not be functional necessary but it should improve readablity. * spike: use correct luxon format token In luxon `DD` means localized date with abbreviated month and `dd` day of the month, padded to 2. * spike: correct naming * spike: remove `firstDayOfWeek` code Currently luxon does not support setting first day of the week (moment/luxon#373) therefore indefinitely removing it. * spike: use `Time` from `@dendronhq/common-all` package `Time` uses `luxon`. * spike(calendar-view): inline defaults There is a case where `config` is `undefined` and then throws at `getMaybeDatePortion`. Until that is fixed we assume default values locally even though they should be defined only in `packages/engine-server/src/config.ts`. * spike: remove `moment` import * spike: fix typescript errors This removes code from the originator. The part that mappes `getShortWeekDays` to starting with Sunday actually rendered a week starting with Monday. Now, with the code removed, it starts with Tuesday. An issue I still need to fix. * spike: move non-page file outside `pages/vscode` nextjs interprets files inside `pages` as actual web pages which `luxon.ts` is not. * spike: display correct weekdays Luxon starts week with Monday indexed as 1. We need to shift so that 1 points to Monday and not Tuesday * chore: fix typo
* spike: add luxon based config generator - The luxon mapping was copied from react-component/picker#230 * spike: fix format string difference Luxon behaves different with format strings. ```tsx const l = DateTime.fromFormat("2021.07", "y.MM.dd") // results in an invalid luxon date l.toFormat("y.MM.dd") // Invalid DateTime const m = moment("2021.07", "y.MM.dd"); // results in an valid moment date m.format("y.MM.DD") // "2021.07.01" ``` * spike: add default value It might not be functional necessary but it should improve readablity. * spike: use correct luxon format token In luxon `DD` means localized date with abbreviated month and `dd` day of the month, padded to 2. * spike: correct naming * spike: remove `firstDayOfWeek` code Currently luxon does not support setting first day of the week (moment/luxon#373) therefore indefinitely removing it. * spike: use `Time` from `@dendronhq/common-all` package `Time` uses `luxon`. * spike(calendar-view): inline defaults There is a case where `config` is `undefined` and then throws at `getMaybeDatePortion`. Until that is fixed we assume default values locally even though they should be defined only in `packages/engine-server/src/config.ts`. * spike: remove `moment` import * spike: fix typescript errors This removes code from the originator. The part that mappes `getShortWeekDays` to starting with Sunday actually rendered a week starting with Monday. Now, with the code removed, it starts with Tuesday. An issue I still need to fix. * spike: move non-page file outside `pages/vscode` nextjs interprets files inside `pages` as actual web pages which `luxon.ts` is not. * spike: display correct weekdays Luxon starts week with Monday indexed as 1. We need to shift so that 1 points to Monday and not Tuesday * chore: fix typo
While this is a quite old issue, I still wanted to share my quick work around: const date = DateTime.fromISO('2021-09-16').startOf('week')
const sunday = date.minus({day: 1}) By simply subtracting a day, you get a sunday which you can use as a start point for further actions. edit: However, once possible, this should be part of the API. |
@lostdesign this is a misleading solution as it would not work when the day is Sunday. If the current day is Sunday, you would want the start of the week day to be today, but |
@lostdesign proposed solution is this:
|
Not quite sure what you mean @bvandercar-vt - your solution acts just the same. The solution proposed will always get the previous sunday which just works as I proposed. If someone needs the current weeks sunday, they should use the solution below. import { DateTime } from 'luxon'
// using the 5th of december which is a sunday
const date = DateTime.fromISO('2021-12-05').setLocale('en-US')
const getSunday =
date.weekdayShort === 'Sun'
? date.toISODate()
: date.startOf('week').minus({day: 1}).toISODate()
// will return the 5th of december
getSunday This again is a work around, as you get the current sunday and can build your own week from that date on to create your sunday-as-first day week, iterating over the date and pushing the following 6 days into an array. I once wrote a method to get the current week dates based of a date that is passed. |
In my case, I need to get the start of the week given any day of the week...
Cheers... |
The topic is especially about the locale-based start of week.. |
I'd just like to chime in here, and say that in the original post, @mars-lan has stated that Sunday is the start of week for Australia. This is incorrect. Australia adheres to ISO 8601-2007 (and in turn, ISO 8601) - which clearly states Monday is the first day of the week. This can be verified in many places, but here is the wikipedia for easy reading: And the definition "[D] is the weekday number, from 1 through 7, beginning with Monday and ending with Sunday." For a brief period, in the official Unicode Common Locale Data Repository (CLDR), Australia's start day was incorrectly changed to Sunday due to a misunderstanding. But after the following case, it was changed back to Monday, which is correct. https://unicode-org.atlassian.net/browse/CLDR-14795 So hopefully Luxon does correctly use Monday as the start of the week for Australia. |
( .weekday%7) for USA/Canada. this will map to [Su Mo Tu We Th Fr Sa], +1 if you are using a grid of 1-7 instead of 0-6 |
In case anyone else is experimenting with this, this is what I am using:
To account for cases where the reference date is Sunday, the reference date is increased by one, the start is calculated, then that date is decreased. I haven't encountered any issues with the approach so far. |
You nailed it! |
The start day of the week in Arab countries is Saturday, so that I came with the following workaround:
|
Don't run that code on Sundays |
I came to the following overlay, which can be imported as patch-file for DateTime. Tip: To import this file, e.g. within angular project, just add
|
Luxon now supports locale-based weeks: |
Thanks for the hint. Unfortunately this is not covered by latest (3.3.5) |
If you must support older browsers without native support, I would argue that a polyfill for the native API is a better option. |
While most countries in the world assume the week starts on Monday, US, Canada, and Australia consider Sunday as the first day of the week. It seems that luxon hard-coded the start of week as Monday.
Note that moment does actually respect the locale setting
The text was updated successfully, but these errors were encountered: