UNPKG

@qntm-code/utils

Version:

A collection of useful utility functions with associated TypeScript types. All functions have been unit tested.

16 lines (15 loc) 739 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getWeekOfYear = void 0; const index_js_1 = require("../convert-units/index.js"); const getStartOfYear_js_1 = require("./getStartOfYear.js"); /** * Gets the week number of the year for the given date. Will use today's date if no date is provided. */ function getWeekOfYear(date = new Date()) { const firstDayOfYear = (0, getStartOfYear_js_1.getStartOfYear)(date); const offset = firstDayOfYear.getDay() + 1; const days = (0, index_js_1.convertTimeUnit)(date.getTime() - firstDayOfYear.getTime(), index_js_1.TimeUnit.Milliseconds, index_js_1.TimeUnit.Days); return Math.ceil((days + offset) / 7); } exports.getWeekOfYear = getWeekOfYear;