@qntm-code/utils
Version:
A collection of useful utility functions with associated TypeScript types. All functions have been unit tested.
12 lines (11 loc) • 543 B
JavaScript
import { TimeUnit, convertTimeUnit } from '../convert-units/index.js';
import { getStartOfYear } from './getStartOfYear.js';
/**
* Gets the week number of the year for the given date. Will use today's date if no date is provided.
*/
export function getWeekOfYear(date = new Date()) {
const firstDayOfYear = getStartOfYear(date);
const offset = firstDayOfYear.getDay() + 1;
const days = convertTimeUnit(date.getTime() - firstDayOfYear.getTime(), TimeUnit.Milliseconds, TimeUnit.Days);
return Math.ceil((days + offset) / 7);
}