Year

year

Extracts the year from a timestamp.

reference/year/year.ts
import { year } from "datezone";

const timestamp = Date.UTC(2024, 0, 1, 12, 0, 0, 0); // 2024-01-01T12:00:00.000Z
const y = year(timestamp, "UTC");
console.log(y); // 2024

isLeapYear

Checks if the year containing the given timestamp is a leap year.

reference/year/isLeapYear.ts
import { isLeapYear } from "datezone";

const timestamp2020 = Date.UTC(2020, 0, 1); // Jan 1, 2020
const timestamp2021 = Date.UTC(2021, 0, 1); // Jan 1, 2021

console.log(isLeapYear(timestamp2020, "UTC")); // true
console.log(isLeapYear(timestamp2021, "UTC")); // false

isLeapYearBase

Checks if a given year is a leap year.

reference/year/isLeapYearBase.ts
import { isLeapYearBase } from "datezone";

console.log(isLeapYearBase(2020)); // true
console.log(isLeapYearBase(2021)); // false

startOfYear

Gets the timestamp for the start of the year containing the given timestamp.

reference/year/startOfYear.ts
import { startOfYear, toISOString } from "datezone";

const ts = Date.UTC(2024, 5, 15); // June 15, 2024
const start = startOfYear(ts, "UTC");
console.log(start, toISOString(start, "UTC")); // 1704067200000 2024-01-01T00:00:00.000Z

endOfYear

Gets the timestamp for the end of the year containing the given timestamp.

reference/year/endOfYear.ts
import { endOfYear, toISOString } from "datezone";

const ts = Date.UTC(2024, 5, 15); // June 15, 2024
const end = endOfYear(ts, "UTC");
console.log(end, toISOString(end, "UTC")); // 1735689599999 2024-12-31T23:59:59.999Z

addYears

Adds the specified number of years to a timestamp. Handles leap year dates appropriately (e.g., Feb 29 -> Feb 28 in non-leap years).

reference/year/addYears.ts
import { addYears, toISOString } from "datezone";

const ts = Date.UTC(2024, 0, 1); // Jan 1, 2024
const twoYearsLater = addYears(ts, 2, "UTC");
console.log(twoYearsLater, toISOString(twoYearsLater, "UTC")); // 1735689600000 2026-01-01T00:00:00.000Z

subYears

Subtracts the specified number of years from a timestamp.

reference/year/subYears.ts
import { subYears, toISOString } from "datezone";

const ts = Date.UTC(2024, 0, 1); // Jan 1, 2024
const twoYearsAgo = subYears(ts, 2, "UTC");
console.log(twoYearsAgo, toISOString(twoYearsAgo, "UTC")); // 1640995200000 2022-01-01T00:00:00.000Z

daysInYear

Gets the number of days in the year containing the given timestamp.

reference/year/daysInYear.ts
import { daysInYear } from "datezone";

const ts2020 = Date.UTC(2020, 0, 1); // Jan 1, 2020
const ts2021 = Date.UTC(2021, 0, 1); // Jan 1, 2021

console.log(daysInYear(ts2020, "UTC")); // 366 (leap year)
console.log(daysInYear(ts2021, "UTC")); // 365 (non-leap year)

daysInYearBase

Gets the number of days in the given year.

reference/year/daysInYearBase.ts
import { daysInYearBase } from "datezone";

console.log(daysInYearBase(2020)); // 366 (leap year)
console.log(daysInYearBase(2021)); // 365 (non-leap year)

quarter

Returns the quarter of the year (1-4) for the given month.

reference/year/quarter.ts
import { quarter } from "datezone";

const quarterNumber = quarter(7);
console.log(quarterNumber); // 3

On this page