The getDay function extracts the day of the month (1-31) from a given Date object or timestamp. It provides validated date access with consistent error handling across Chronia’s API surface.
function getDay(date: DateInput): number;
| Parameter | Type | Description |
|---|---|---|
date |
DateInput |
A Date object, numeric timestamp, or ISO 8601 string from which to extract the day |
| Type | Description |
|---|---|
number |
The day of the month (1-31), or NaN if the input is invalid |
The getDay function extracts and returns the day of the month from the provided date. It validates the input before processing and returns the day in the local timezone. The function accepts both Date objects and numeric timestamps for flexibility.
Date object with a valid dateNaN when:new Date('invalid'))NaNInfinity-InfinityisValidDateOrNumber) as other Chronia functionsNaNDate | numberimport { getDay } from "chronia";
// Extract day from Date object
const date = new Date(2025, 0, 15);
const day = getDay(date); // Returns: 15
// Display formatted date component
console.log(`Day: ${day}`); // Output: "Day: 15"
// Get day from timestamp (note: uses local timezone)
const timestamp = 1704067200000; // 2024-01-01T00:00:00.000Z in UTC
const day2 = getDay(timestamp); // Returns: 1 (may vary by timezone)
import { getDay } from "chronia";
// Check if we're in the first half of the month
function isFirstHalfOfMonth(date: Date | number): boolean {
const day = getDay(date);
return day <= 15;
}
const date1 = new Date(2025, 0, 10);
isFirstHalfOfMonth(date1); // Returns: true
const date2 = new Date(2025, 0, 20);
isFirstHalfOfMonth(date2); // Returns: false
// Calculate days until end of month
function daysUntilMonthEnd(date: Date, daysInMonth: number): number {
const day = getDay(date);
return daysInMonth - day;
}
import { getDay } from "chronia";
// Check if date is the first day of the month
function isFirstDayOfMonth(date: Date | number): boolean {
return getDay(date) === 1;
}
isFirstDayOfMonth(new Date(2025, 0, 1)); // Returns: true
isFirstDayOfMonth(new Date(2025, 0, 15)); // Returns: false
// Check if date is a specific billing day
function isBillingDay(date: Date | number): boolean {
const day = getDay(date);
return day === 1 || day === 15; // Bills on 1st and 15th
}
isBillingDay(new Date(2025, 0, 1)); // Returns: true
isBillingDay(new Date(2025, 0, 15)); // Returns: true
isBillingDay(new Date(2025, 0, 10)); // Returns: false
import { getDay } from "chronia";
// Leap day handling
const leapDay = new Date(2024, 1, 29); // February 29, 2024
getDay(leapDay); // Returns: 29
// End of month variations
const jan31 = new Date(2025, 0, 31); // January 31
getDay(jan31); // Returns: 31
const feb28 = new Date(2025, 1, 28); // February 28, 2025 (non-leap)
getDay(feb28); // Returns: 28
// Invalid date handling
const invalid = new Date("invalid");
getDay(invalid); // Returns: NaN
// Safe usage with validation
function safeGetDay(date: Date | number): number | null {
const day = getDay(date);
return isNaN(day) ? null : day;
}
safeGetDay(new Date(2025, 0, 15)); // Returns: 15
safeGetDay(new Date("invalid")); // Returns: null
import { getDay } from "chronia";
// Generate calendar grid for current month
function generateMonthDays(year: number, month: number): number[] {
const days: number[] = [];
const daysInMonth = new Date(year, month + 1, 0).getDate();
for (let i = 1; i <= daysInMonth; i++) {
const date = new Date(year, month, i);
days.push(getDay(date));
}
return days;
}
// Returns: [1, 2, 3, ..., 31] for January
const januaryDays = generateMonthDays(2025, 0);
// Highlight specific day in calendar
function isHighlightedDay(date: Date, targetDay: number): boolean {
return getDay(date) === targetDay;
}
const today = new Date(2025, 0, 15);
isHighlightedDay(today, 15); // Returns: true