-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
ISO weeks (start from monday)? #215
Comments
Check isoWeek here https://day.js.org/docs/en/get-set/iso-week |
@iamkun yes https://momentjs.com/docs/#/get-set/weekday/ I think it would be nice to define the first day of week globally by locale or plugin |
Should be defined in locale file I think. |
I could work on this |
That would be nice. What's your plan then? |
let me dive in the code for a couple of days first |
Cool THX. |
Well regardless of the locale (depending on the user settings), I need to be able to get the start of the week to be monday (like the iso). In moment:
|
@iamkun here is my plan:
I think adding new constant |
And for those (like me) that want all locale weeks to start on Sunday or Monday they can override the mondayFirst parameter (is it easy ?). That way no need of an isoWeek. |
@hlehmann you mean the case when you want to use texts from some locale that starts from Sunday, but set "mondayFirst" in config (not in the locale)? |
Yes something like this
|
better in locale file Plus, I'm looking for a way to implement this as a plugin, rather in the main code. |
I would suggest to instead of defining an attribute |
Any news here? |
Subscribe to this issue to remove ternary operators after it will be fixed iamkun/dayjs#215
Subscribe to this issue to remove ternary operators after it will be fixed iamkun/dayjs#215 Bugfixes ===== Start week from sunday for "en" locale
Subscribe to this issue to remove ternary operators after it will be fixed iamkun/dayjs#215 Bugfixes ===== Start week from sunday for "en" locale
Any update? Here is my quick workaround: const dayjs = require('dayjs')
const current_millis = dayjs().valueOf()
const add_day = (count, millis) => dayjs(millis).add(count, 'day').valueOf()
// ===== the actual "week starts on" stuff =====
const start_of_week = (millis, week_starts_on) => {
if (week_starts_on === 'sunday') {
return dayjs(millis).startOf('week').valueOf()
} else if (week_starts_on === 'monday') {
return add_day(1, dayjs(millis).subtract(1, 'day').startOf('week').valueOf())
}
}
const end_of_week = (millis, week_starts_on) => {
if (week_starts_on === 'sunday') {
return dayjs(millis).endOf('week').valueOf()
} else if (week_starts_on === 'monday') {
return add_day(1, dayjs(millis).subtract(1, 'day').endOf('week').valueOf())
}
}
// =====
console.log('===== week start =====')
console.log(`week starts on sunday: ${dayjs(start_of_week(current_millis, 'sunday')).format('DD MMM YYYY')}`)
console.log(`week starts on monday: ${dayjs(start_of_week(current_millis, 'monday')).format('DD MMM YYYY')}`)
console.log('===== week end =====')
console.log(`week ends on sunday: ${dayjs(end_of_week(current_millis, 'sunday')).format('DD MMM YYYY')}`)
console.log(`week ends on monday: ${dayjs(end_of_week(current_millis, 'monday')).format('DD MMM YYYY')}`) |
🎉 This issue has been resolved in version 1.8.4 🎉 The release is available on: Your semantic-release bot 📦🚀 |
I found 2 solutions to override the weekStart globally (per locale). I don't know if this is the best way but maybe this helps somebody else too: Solution 1: import 'dayjs' from 'dayjs';
dayjs.Ls.en.weekStart = 1; Solution 2: import 'dayjs' from 'dayjs';
import en from 'dayjs/locale/en';
dayjs.locale({
...en,
weekStart: 1,
}); |
@hoodwink73 Thanks. But seems we don't have to mention this, do we? |
I had to dig in the source code to find this two solutions. Adding this to the docs would be better. Some one who needs this feature may not have the time, patience or skills to search in the soure code and may decide to use another library. But which of my solutions is better for documentation? The first one is shorter but I don't think that the |
Solution 2 is better I think. However, a new plugin 'isoWeek' should be made to fix this issue is better than the temporary solution above. |
A plugin would be a suitable solution as well. But it should still have the option to set different |
According to moment.js, Gets or sets the ISO day of the week with 1 being Monday and 7 being Sunday. There's no other option cause it's ISO standard. |
I think the current solution is great and if you need to redefine weekStart for some reason, merging locale fits perfectly in the current API (as in the example above) dayjs.locale({
...en,
weekStart: 1,
}); |
Also needed import { Dayjs, PluginFunc } from 'dayjs'
declare module 'dayjs' {
interface Dayjs {
/**
* 1: Monday
* ...
* 6: Saturday
* 7: Sunday
*/
isoWeekday (): number
}
}
const isoWeekday: PluginFunc = (_opt, dayjsClass) => {
dayjsClass.prototype.isoWeekday = function (this: Dayjs) {
const { $W } = this as any
return $W <= 0 ? $W + 7 : $W
}
}
export default isoWeekday |
Hello @iamkun. I approved that it should be handled in |
add support |
Hi, question: in all locales/settings
|
This one works perfectly, will be very helpful for react app where I need to load it globally on beginning. While using the plugin doesn't seem to work on my other calendar if defined at the top level app |
With dayJS v1.10.7, following Solution 2 breaks support for ordinals "Do" (?st, ?nd, ?rd) when formatting with the error Using Solution 1 retains ordinal functionality and shifts the weekday to Monday. |
A better way to update:
|
There are some handy methods in moment.js like
.startOf('isoWeek')
,.isoWeekday(1)
. They are very useful when Monday is the first day of week in current locale.I didn't found any similar methods, are they exists?
The text was updated successfully, but these errors were encountered: