dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

`weekdays` are incorrect for all instances of `en` locale (except US if it existed)

Open ortonomy opened this issue 2 years ago • 8 comments

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

ortonomy avatar May 09 '22 05:05 ortonomy

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.

BePo65 avatar Jun 05 '22 18:06 BePo65

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("_")

emaborsa avatar Jul 08 '22 06:07 emaborsa

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 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.

divStar avatar Oct 23 '22 00:10 divStar

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)
})

BePo65 avatar Oct 23 '22 05:10 BePo65

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?

emaborsa avatar Oct 25 '22 06:10 emaborsa

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]

BePo65 avatar Oct 26 '22 17:10 BePo65