The addHours function adds a specified number of hours to a given date, returning a new Date object with the hours added. It provides a safe way to perform hour-based date arithmetic while preserving other time components and handling edge cases gracefully.
function addHours(date: DateInput, amount: number): Date;
| Parameter | Type | Description |
|---|---|---|
date |
DateInput |
The base date as a Date object, numeric timestamp, or ISO 8601 string |
amount |
number |
The number of hours to add (can be negative to subtract) |
| Type | Description |
|---|---|
Date |
A new Date object with the specified hours added, or Invalid Date if any input is invalid |
The addHours function performs hour-based date arithmetic by adding (or subtracting) a specified number of hours to a given date. The function validates all inputs before processing and returns a new Date instance, ensuring immutability of the original date. Fractional hour values are truncated toward zero, and all other time components (minutes, seconds, milliseconds) are preserved.
date argument is a valid Date object (not Invalid Date)date argument is a finite numeric timestamp, including:
0, representing January 1, 1970, 00:00:00 UTC)amount argument is a finite number (can be positive, negative, or zero)Math.trunc)date argument is an Invalid Date object (e.g., new Date('invalid'))date argument is NaN, Infinity, or -Infinityamount argument is NaN, Infinity, or -InfinityisValidDateOrNumber and isValidNumber)Math.trunc (e.g., 1.9 becomes 1, -1.9 becomes -1)Date | number for date and number for amountDate.setHours() and Date.getHours() methods for reliable date arithmeticimport { addHours } from "chronia";
// Schedule a meeting 3 hours from now
const now = new Date(2025, 0, 15, 14, 30, 0);
const meetingTime = addHours(now, 3);
// Returns: 2025-01-15 17:30:00
// Calculate a deadline 24 hours from now
const deadline = addHours(new Date(), 24);
// Look back 2 hours for recent activity
const twoHoursAgo = addHours(new Date(), -2);
import { addHours } from "chronia";
// Convert UTC to EST (UTC-5)
const utcTime = new Date(Date.UTC(2025, 0, 15, 20, 0, 0));
const estTime = addHours(utcTime, -5);
// Returns: 2025-01-15 15:00:00 (EST)
// Convert PST to JST (PST+17)
const pstTime = new Date(2025, 0, 15, 9, 0, 0);
const jstTime = addHours(pstTime, 17);
// Returns: 2025-01-16 02:00:00 (JST, crosses day boundary)
import { addHours } from "chronia";
// Set session expiration to 2 hours from now
function createSession() {
const now = new Date();
const expiresAt = addHours(now, 2);
return {
id: generateId(),
createdAt: now,
expiresAt: expiresAt,
};
}
// Check if token is still valid (expires in 24 hours)
function isTokenValid(tokenCreatedAt: Date): boolean {
const expirationTime = addHours(tokenCreatedAt, 24);
return new Date() < expirationTime;
}
import { addHours } from "chronia";
// Calculate closing time from opening time
const openingTime = new Date(2025, 0, 15, 9, 0, 0);
const closingTime = addHours(openingTime, 8);
// Returns: 2025-01-15 17:00:00 (8-hour work day)
// Calculate end of night shift (crosses midnight)
const shiftStart = new Date(2025, 0, 15, 22, 0, 0);
const shiftEnd = addHours(shiftStart, 8);
// Returns: 2025-01-16 06:00:00 (next day)
import { addHours } from "chronia";
// Fractional hours are truncated
const result1 = addHours(new Date(2025, 0, 1, 12, 0, 0), 1.9);
// Returns: 2025-01-01 13:00:00 (1.9 truncated to 1)
const result2 = addHours(new Date(2025, 0, 1, 12, 0, 0), -1.9);
// Returns: 2025-01-01 11:00:00 (-1.9 truncated to -1)
// Minutes, seconds, and milliseconds are preserved
const precise = new Date(2025, 0, 1, 12, 34, 56, 789);
const adjusted = addHours(precise, 3);
// Returns: 2025-01-01 15:34:56.789
// Invalid inputs return Invalid Date
const invalid1 = addHours(new Date("invalid"), 3);
// Returns: Invalid Date
const invalid2 = addHours(new Date(), NaN);
// Returns: Invalid Date
const invalid3 = addHours(new Date(), Infinity);
// Returns: Invalid Date
// Zero amount returns new Date with same time
const original = new Date(2025, 0, 1, 12, 0, 0);
const copy = addHours(original, 0);
// Returns: 2025-01-01 12:00:00 (new instance, same time)
// Works with numeric timestamps
const timestamp = 1704110400000; // 2024-01-01 12:00:00 UTC
const adjusted2 = addHours(timestamp, 5);
// Returns: 2024-01-01 17:00:00 UTC