dayOfYear
A utility function to calculate the day of the year for a given date.> dayOfYear(date: Date = new Date()): number
- date: The date for which to calculate the day of the year. Defaults to the current date if not provided.
- Returns: The day of the year as a number (1-366). If the date is invalid, it will return `NaN`.
Example
import { dayOfYear } from "@explita/daily-toolset";
// Get the day of the year for a specific date
const specificDate = new Date("2024-03-15");
console.log(dayOfYear(specificDate)); // Example output: 75
// Get the current day of the year
const today = new Date();
console.log(dayOfYear(today)); // Example output: the current day of the year
// Check with an invalid date
const invalidDate = new Date("invalid date string");
console.log(dayOfYear(invalidDate)); // NaN
Use Cases
- Determining the day number in the current year for calculating progress or deadlines.
- Creating date-based reports where you need to know the day of the year for specific events.
- Calculating statistics like how many days have passed since the beginning of the year.