@technobuddha/library
Version:
A large library of useful functions
20 lines (18 loc) • 637 B
text/typescript
import { beginningOfYear } from './beginning-of-year.ts';
import { type DateOptions, ticksPerDay } from './date.ts';
import { floor } from './floor.ts';
/**
* Calculates the day of the year for a given date.
* @param input - The date for which to calculate the day of the year.
* @param options - Optional settings.
* @returns The day of the year as a number (1-based).
* @group Time
* @category Year
*/
export function dayOfYear(input: Date, { utc = false }: DateOptions = {}): number {
return (
floor((input.getTime() - beginningOfYear(input, { utc }).getTime()) / ticksPerDay, {
tolerance: 0.05,
}) + 1
);
}