dayjs
dayjs copied to clipboard
`weekdays` are incorrect for all instances of `en` locale (except US if it existed)
Describe the bug
weekdays
returns weekdays:"Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_")
on all instances of en
locale
Expected behavior
weekdays
returns weekdays:"Monday_Tuesday_Wednesday_Thursday_Friday_Saturday_Sunday".split("_")
for all instances of en locale that aren't the US. Not everyone in anglophone world is American and not everyone has backward-ass date notations.
Information
- latest
- N/A
- N/A
- N/A
it is correct that dayjs.weekdays() returns an array with the names of the weekdays. In the 'en' locale this list looks like:
[
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
]
The sequence of the names does not imply that the first entry is the first day of the week, but just a way of finding the correct name of a weekday. So there is no need to change that.
I think the order of the returned names should reflect the weekStart
of the local setting:
dayjs.locale({ ...de, weekStart: 1 });
should return weekdays:"Montag_Dienstag_Mittwoch_Donnerstag_Freitag_Samstag_Sonntag".split("_")
I think the order of the returned names should reflect the
weekStart
of the local setting:
dayjs.locale({ ...de, weekStart: 1 });
should returnweekdays:"Montag_Dienstag_Mittwoch_Donnerstag_Freitag_Samstag_Sonntag".split("_")
I absolutely agree with that. How else would I be able to find the corresponding weekday to a given index? I mean - it's possible to just assume weekStart: 0
when mapping weekdays to indices, but doesn't this make it somewhat more complicated?
Besides: in a lot of countries Monday is generally the first day of the week.
Just simply by using the weekday plugin.
The internal representation (used e.g.for 'format()') is not affected by the locale. Therefore dayjs().format('dddd')
will always return the desired output; even when the first day of the week is Monday.
I verified this with a little test:
import MockDate from 'mockdate'
import dayjs from '../src'
import localeData from '../src/plugin/localeData'
import '../src/locale/en-gb'
dayjs.extend(localeData)
dayjs.locale('en-gb')
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('should return formatted day of week', () => {
const dateString = '2022-10-23T10:11:12'
const testDate = dayjs(dateString)
const globalLocaleData = dayjs.localeData()
expect(testDate.format('dddd')).toBe('Sunday')
expect(testDate.day()).toBe(0)
expect(globalLocaleData.firstDayOfWeek()).toBe(1)
})
I understand, but I am not working with a specific day, I am working with the weekdays
names. How can I get the correct order of these names, based on weekStart
?
in the following code fragment
const firstDayOfWeek = dayjs.localeData()
const weekdayNames = dayjs.weekdays()
for (let i = 0; i < 7; i++) {
console.log(weekdayNames[i])
}
instead of using weekdayNames[i]
just use weekdayNames[(i + firstDayOfWeek) % 7]