weekOfYear

A utility function to calculate the week number of the year for a given date.
> weekOfYear(date: Date = new Date()): number
Parameters
  • date: The date for which to calculate the week number. Defaults to the current date if not provided.
  • Returns: The week number of the year as a number (1-53). If the date is invalid, it will return `NaN`.

Example

import { weekOfYear } from "@explita/daily-toolset";

// Get the week number of the year for a specific date
const specificDate = new Date("2024-03-15");
console.log(weekOfYear(specificDate)); // Example output: 11

// Get the current week number of the year
const today = new Date();
console.log(weekOfYear(today)); // Example output: current week number

// Check with an invalid date
const invalidDate = new Date("invalid date string");
console.log(weekOfYear(invalidDate)); // NaN

Use Cases

  • Determining the week number for calculating progress or deadlines in a year.
  • Creating weekly reports or time-based statistics.
  • Calculating how far into the year a given date is.