@progress/kendo-date-math
Version:
Kendo UI typescript package exporting functions for Date manipulations
50 lines (49 loc) • 1.97 kB
JavaScript
import { Day } from './day.enum';
import { addDays } from './add-days';
import { createDate } from './create-date';
import { prevDayOfWeek } from './prev-day-of-week';
import { MS_PER_DAY } from './constants';
import { getDate } from './get-date';
const moveDateToWeekStart = (date, weekStartDay) => {
if (weekStartDay !== Day.Monday) {
return addDays(prevDayOfWeek(date, weekStartDay), 4);
}
return addDays(date, (4 - (date.getDay() || 7)));
};
const calcWeekInYear = (date, weekStartDay) => {
const firstWeekInYear = createDate(date.getFullYear(), 0, 1, -6);
const newDate = moveDateToWeekStart(date, weekStartDay);
const diffInMS = newDate.getTime() - firstWeekInYear.getTime();
const days = Math.floor(diffInMS / MS_PER_DAY);
return 1 + Math.floor(days / 7);
};
/**
* A function that returns the number of the week within a year, which is calculated in relation to the date.
*
* For more information, refer to the [**ISO week date**](https://en.wikipedia.org/wiki/ISO_week_date) article.
*
* @param date - The date used for the week number calculation.
* @param weekStartDay - The first day of the week. By default, the first week day is Monday.
* @returns - The number of the week within the year.
*
* @example
* ```ts-no-run
* weekInYear(new Date(2016, 0, 1)); // Week 53, 2015
* weekInYear(new Date(2016, 0, 5)); // Week 1, 2016
* weekInYear(new Date(2017, 0, 1)); // Week 52, 2016
* weekInYear(new Date(2017, 0, 2)); // Week 1, 2017
* ```
*/
export const weekInYear = (date, weekStartDay = Day.Monday) => {
date = getDate(date);
const prevWeekDate = addDays(date, -7);
const nextWeekDate = addDays(date, 7);
const weekNumber = calcWeekInYear(date, weekStartDay);
if (weekNumber === 0) {
return calcWeekInYear(prevWeekDate, weekStartDay) + 1;
}
if (weekNumber === 53 && calcWeekInYear(nextWeekDate, weekStartDay) > 1) {
return 1;
}
return weekNumber;
};