Month
month
Gets the month of the year for a given timestamp.
import { month } from "datezone";
const timestamp = 1720612800000; // 2024-07-10T12:00:00.000Z
const m = month(timestamp, "America/New_York");
console.log(m); // 7
startOfMonth
Gets the timestamp for the start of the month containing the given timestamp.
import { startOfMonth } from "datezone";
const timestamp = 1720612800000; // 2024-07-10T12:00:00.000Z
const start = startOfMonth(timestamp, "America/New_York");
console.log(start); // 1719806400000 (2024-07-01T04:00:00.000Z)
startOfMonthBase
Gets the timestamp for the start of the month for given year and month.
import { startOfMonthBase } from "datezone";
const start = startOfMonthBase(2024, 7, "America/New_York");
console.log(start); // 1719806400000 (2024-07-01T04:00:00.000Z)
endOfMonth
Gets the timestamp for the end of the month containing the given timestamp.
import { endOfMonth } from "datezone";
const timestamp = 1720612800000; // 2024-07-10T12:00:00.000Z
const end = endOfMonth(timestamp, "America/New_York");
console.log(end); // 1722484799999 (2024-08-01T03:59:59.999Z)
endOfMonthBase
Gets the timestamp for the end of the month for given year and month.
import { endOfMonthBase } from "datezone";
const end = endOfMonthBase(2024, 7, "America/New_York");
console.log(end); // 1722484799999 (2024-08-01T03:59:59.999Z)
addMonths
Adds the specified number of months to a timestamp. Handles month-end dates appropriately (e.g., Jan 31 + 1 month = Feb 28/29).
import { addMonths } from "datezone";
const timestamp = 1720612800000; // 2024-07-10T12:00:00.000Z
const twoMonthsLater = addMonths(timestamp, 2, "America/New_York");
console.log(twoMonthsLater); // 1725969600000 (2024-09-10T12:00:00.000Z)
addMonthsBase
Adds the specified number of months to a timestamp with calendar components.
import { addMonthsBase } from "datezone";
const twoMonthsLater = addMonthsBase(
2024, // Year
7, // Month
10, // Day
12, // Hour
0, // Minute
0, // Second
0, // Millisecond
2, // Months to add
"America/New_York", // Time zone
);
console.log(twoMonthsLater); // 1725969600000 (2024-09-10T12:00:00.000Z)
subMonths
Subtracts the specified number of months from a timestamp.
import { subMonths } from "datezone";
const timestamp = 1720612800000; // 2024-07-10T12:00:00.000Z
const newDate = subMonths(timestamp, 2, "America/New_York");
console.log(newDate); // 1715342400000 (2024-05-10T12:00:00.000Z)
startOfNthMonth
Gets the start of the nth month relative to the given timestamp.
import { startOfNthMonth } from "datezone";
const timestamp = 1720612800000; // 2024-07-10T12:00:00.000Z
const start = startOfNthMonth(timestamp, 2, "America/New_York");
console.log(start); // 1725163200000 (2024-09-01T04:00:00.000Z)
endOfNthMonth
Gets the end of the nth month relative to the given timestamp.
import { endOfNthMonth } from "datezone";
const timestamp = 1720612800000; // 2024-07-10T12:00:00.000Z
const end = endOfNthMonth(timestamp, 2, "America/New_York");
console.log(end); // 1727755199999 (2024-10-01T03:59:59.999Z)
startOfNextMonth
Gets the start of the next month relative to the given timestamp.
import { startOfNextMonth } from "datezone";
const timestamp = 1720612800000; // 2024-07-10T12:00:00.000Z
const start = startOfNextMonth(timestamp, "America/New_York");
console.log(start); // 1722484800000 (2024-08-01T04:00:00.000Z)
endOfNextMonth
Gets the end of the next month relative to the given timestamp.
import { endOfNextMonth } from "datezone";
const timestamp = 1720612800000; // 2024-07-10T12:00:00.000Z
const end = endOfNextMonth(timestamp, "America/New_York");
console.log(end); // 1725163199999 (2024-09-01T03:59:59.999Z)
startOfPrevMonth
Gets the start of the previous month relative to the given timestamp.
import { startOfPrevMonth } from "datezone";
const timestamp = 1720612800000; // 2024-07-10T12:00:00.000Z
const start = startOfPrevMonth(timestamp, "America/New_York");
console.log(start); // 1717214400000 (2024-06-01T04:00:00.000Z)
endOfPrevMonth
Gets the end of the previous month relative to the given timestamp.
import { endOfPrevMonth } from "datezone";
const timestamp = 1720612800000; // 2024-07-10T12:00:00.000Z
const end = endOfPrevMonth(timestamp, "America/New_York");
console.log(end); // 1719806399999 (2024-07-01T03:59:59.999Z)
daysInMonth
Gets the number of days in a month for a given timestamp.
import { daysInMonth } from "datezone";
const timestamp = 1720612800000; // 2024-07-10T12:00:00.000Z
const days = daysInMonth(timestamp, "America/New_York");
console.log(days); // 31
daysInMonthBase
Gets the number of days in a month for given year and month.
import { daysInMonthBase } from "datezone";
const days = daysInMonthBase(2024, 7);
console.log(days); // 31
calculateYearMonth
Calculates the year and month after adding a specified number of months.
import { calculateYearMonth } from "datezone";
const [year, month] = calculateYearMonth(2024, 7, 2);
console.log({ month, year }); // { month: 9, year: 2024, }
getMonthName
Gets the localized name of a month.
import { getMonthName } from "datezone";
const name = getMonthName("en-US", "long", 7);
console.log(name); // "July"
const name2 = getMonthName("es-ES", "short", 7);
console.log(name2); // "jul"
const name3 = getMonthName("en-US", "narrow", 7);
console.log(name3); // "J"
getQuarter
Gets the quarter (1-4) for a given timestamp.
import { getQuarter } from "datezone";
const timestamp = 1720612800000; // 2024-07-10T12:00:00.000Z
const quarter = getQuarter(timestamp, "America/New_York");
console.log(quarter); // 3
getQuarterBase
Gets the quarter (1-4) for a given month.
import { getQuarterBase } from "datezone";
const quarter = getQuarterBase(7);
console.log(quarter); // 3