dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

`dayjs.week` returns wrong number.

Open adamazad opened this issue 3 years ago • 1 comments

Describe the bug

I have this function :

/**
 * Get the week information for the current week in UTC
 * @param week - The week date to get the information for. Must be in YYYY-MM-DD format.
 * @throws Error if the date is not valid
 */
export function getWeekInformation(date?: string): WeekInformation {
  // If a week is provided, validate
  if (date !== undefined && !isValidDate(date)) {
    throw new Error(`Invalid date: ${week}. Accepted format: YYYY-MM-DD`);
  }

  // Get this week's Monday 00:00:00 UTC
  const thisWeekStart = dayjs(date).startOf('week').utc(false);

  if (!thisWeekStart.isValid()) {
    throw new Error('Invalid week');
  }

  // Get this week's Sunday 11:59:00 UTC
  const thisWeekEnd = thisWeekStart.endOf('week');

  return {
    weekDate: thisWeekStart.format('YYYY-MM-DD'),
    startDate: thisWeekStart,
    endDate: thisWeekEnd,
    year: thisWeekStart.year(),
    weekNumber: thisWeekStart.week()
  };
}

weekNumber is always increased by one. For instance:

dayjs('2022-12-31').week() // returns 53 

Expected behavior

There are 52 weeks in a year, the above-isolated snippet should return 52.

Information

  • Day.js Version: v1.11.3
  • OS: macOS
  • Browser: Node v16.14
  • Time zone: Western Europe

adamazad avatar Aug 31 '22 20:08 adamazad

The year 2020 has a week numbered '53' (is a consequence of what weekday a year starts with) - details can be found e.g. in rosecalendars.

BePo65 avatar Sep 05 '22 09:09 BePo65