The endOfMonth function returns a new Date object set to the last day of the month at 23:59:59.999 for the given date. It automatically handles different month lengths and leap years while preserving the year and month of the input date.
function endOfMonth(date: DateInput): Date;
| Parameter | Type | Description |
|---|---|---|
date |
DateInput |
The base date as a Date object, numeric timestamp, or ISO 8601 string |
| Type | Description |
|---|---|
Date |
A new Date object set to the last day of the month at 23:59:59.999, or Invalid Date if input is invalid |
The endOfMonth function calculates the last moment of the month for the provided date. It takes any date within a month and returns a new Date object representing the final millisecond of that month (23:59:59.999 on the last day). The function intelligently handles varying month lengths and leap year February dates.
Date object (not Invalid Date)0, representing January 1, 1970, 00:00:00 UTC)The returned Date will have:
new Date('invalid'))NaNInfinity-Infinity0 of the next month equals the last day of the current monthDate | numberstartOfMonth. Useful for calendar applications, analytics dashboards, and time-based queries.import { endOfMonth } from "chronia";
// Get end of month from mid-month date
const midMonth = new Date(2024, 5, 15, 14, 30, 45);
const monthEnd = endOfMonth(midMonth);
// Returns: June 30, 2024 23:59:59.999
// Works with first day of month
const firstDay = new Date(2024, 0, 1, 0, 0, 0);
const januaryEnd = endOfMonth(firstDay);
// Returns: January 31, 2024 23:59:59.999
// Works with last day of month
const lastDay = new Date(2024, 11, 31);
const decemberEnd = endOfMonth(lastDay);
// Returns: December 31, 2024 23:59:59.999
import { endOfMonth } from "chronia";
// 31-day month (January)
endOfMonth(new Date(2024, 0, 10));
// Returns: January 31, 2024 23:59:59.999
// 30-day month (April)
endOfMonth(new Date(2024, 3, 10));
// Returns: April 30, 2024 23:59:59.999
// 28-day month (February in non-leap year)
endOfMonth(new Date(2023, 1, 10));
// Returns: February 28, 2023 23:59:59.999
// 29-day month (February in leap year)
endOfMonth(new Date(2024, 1, 10));
// Returns: February 29, 2024 23:59:59.999
import { endOfMonth } from "chronia";
// Create a month range for queries
function getMonthRange(date: Date) {
const start = new Date(date.getFullYear(), date.getMonth(), 1, 0, 0, 0, 0);
const end = endOfMonth(date);
return { start, end };
}
const range = getMonthRange(new Date(2024, 5, 15));
// Returns: {
// start: June 1, 2024 00:00:00.000,
// end: June 30, 2024 23:59:59.999
// }
// Use for database queries or filtering
function getMonthlyData(
date: Date,
data: Array<{ timestamp: Date; value: number }>,
) {
const { start, end } = getMonthRange(date);
return data.filter(
(item) => item.timestamp >= start && item.timestamp <= end,
);
}
import { endOfMonth } from "chronia";
// Works with numeric timestamps
const timestamp = Date.now();
const currentMonthEnd = endOfMonth(timestamp);
// Returns: Last day of current month at 23:59:59.999
// Unix epoch timestamp
const epochEnd = endOfMonth(0);
// Returns: January 31, 1970 23:59:59.999
// Historical dates (negative timestamps)
const historicalEnd = endOfMonth(new Date(1969, 11, 15).getTime());
// Returns: December 31, 1969 23:59:59.999
import { endOfMonth } from "chronia";
// Set deadline to end of current month
function setMonthEndDeadline(): Date {
return endOfMonth(new Date());
}
// Check if a date is within the current month
function isInCurrentMonth(date: Date): boolean {
const now = new Date();
const monthStart = new Date(now.getFullYear(), now.getMonth(), 1);
const monthEnd = endOfMonth(now);
return date >= monthStart && date <= monthEnd;
}
isInCurrentMonth(new Date(2024, 5, 15)); // Returns: true (if current month is June 2024)
isInCurrentMonth(new Date(2024, 6, 1)); // Returns: false (if current month is June 2024)
import { endOfMonth } from "chronia";
// Invalid inputs return Invalid Date
endOfMonth(new Date("invalid")); // Returns: Invalid Date
endOfMonth(NaN); // Returns: Invalid Date
endOfMonth(Infinity); // Returns: Invalid Date
endOfMonth(-Infinity); // Returns: Invalid Date
// Validate result before using
function safeEndOfMonth(date: Date | number): Date | null {
const result = endOfMonth(date);
return isNaN(result.getTime()) ? null : result;
}
safeEndOfMonth(new Date(2024, 5, 15)); // Returns: June 30, 2024 23:59:59.999
safeEndOfMonth(new Date("invalid")); // Returns: null