date-io icon indicating copy to clipboard operation
date-io copied to clipboard

Inconsistent getWeekdays between dayjs and other libs

Open vadzimmaisiuk opened this issue 7 years ago • 1 comments

In @date-io/date-fns getWeekdays returns an array of weekdays with respect to user locale: https://github.com/dmtrKovalenko/date-io/blob/master/packages/date-fns/src/date-fns-utils.ts#L227

In @date-io/dayjs getWeekdays returns an array of weekdays always starting with sunday. https://github.com/dmtrKovalenko/date-io/blob/master/packages/dayjs/src/dayjs-utils.ts#L209 This happens, because in dayjs `set('day', 0) always yields sunday (source: dayjs docs)

Possible solution to this is to use dayjs().startOf('week'), which results in correct week start for the given locale

vadzimmaisiuk avatar Aug 11 '19 16:08 vadzimmaisiuk

Here's an example getWeekdays implementation that works correctly with ru locale

public getWeekdays() {
  const start = this.dayjs().startOf("week");
  return [0, 1, 2, 3, 4, 5, 6].map(diff =>
    this.format(this.dayjs(start).add(diff, "day"), "dd")
  );
}

vadzimmaisiuk avatar Aug 11 '19 16:08 vadzimmaisiuk