@progress/kendo-date-math
Version:
Kendo UI typescript package exporting functions for Date manipulations
23 lines (22 loc) • 694 B
JavaScript
import { cloneDate } from './clone-date';
import { Day } from './day.enum';
/**
* A function which returns the first date of the current week.
*
* @param date - The initial date.
* @param weekStartDay [default: Day.Sunday] - The first day of the week.
* @returns - The first date of the current week.
*
* @example
* ```ts-no-run
* firstDayInWeek(new Date(2016, 0, 15)); // 2016-01-10
* firstDayInWeek(new Date(2016, 0, 15), Day.Monday); // 2016-01-11
* ```
*/
export const firstDayInWeek = (date, weekStartDay = Day.Sunday) => {
const first = cloneDate(date);
while (first.getDay() !== weekStartDay) {
first.setDate(first.getDate() - 1);
}
return first;
};