The setSeconds function sets the seconds component of a given Date object or timestamp, returning a new Date instance with the updated value. It provides validation and handles fractional values and rollover behavior consistently with Chronia’s API design.
function setSeconds(date: DateInput, seconds: number): Date;
| Parameter | Type | Description |
|---|---|---|
date |
DateInput |
A Date object, numeric timestamp, or ISO 8601 string representing the base date |
seconds |
number |
The seconds value to set (0-59 for normal range, other values will roll over) |
| Type | Description |
|---|---|
Date |
A new Date object with the seconds component set to the specified value, or Invalid Date if any input is invalid |
The setSeconds function creates a new Date object by setting the seconds component of the provided date to the specified value. It validates both arguments before processing and handles fractional seconds by truncating them toward zero. The function preserves all other time components (year, month, day, hours, minutes, and milliseconds) while updating only the seconds.
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)seconds argument is a finite number (including negative values, zero, or values > 59)seconds values are provided (truncated toward zero using Math.trunc)date argument is an Invalid Date object (e.g., new Date('invalid'))date argument is NaN, Infinity, or -Infinityseconds argument is NaN, Infinity, or -InfinityMath.trunc (30.9 → 30, -30.9 → -30)Date | number for date and number for secondsimport { setSeconds } from "chronia";
// Reset seconds to 0 (start of minute)
const now = new Date(2025, 0, 15, 12, 30, 45, 500);
const normalized = setSeconds(now, 0);
// Returns: 2025-01-15 12:30:00.500 (milliseconds preserved)
// Set to 30 seconds
const halfMinute = setSeconds(new Date(2025, 0, 15, 12, 30, 45), 30);
// Returns: 2025-01-15 12:30:30
// Using numeric timestamp
const timestamp = 1705324245000; // Some timestamp
const updated = setSeconds(timestamp, 0);
// Returns: Date with seconds set to 0
import { setSeconds } from "chronia";
// Update specific second value
function setSecondsFromInput(baseDate: Date, userSeconds: number): Date {
return setSeconds(baseDate, userSeconds);
}
const base = new Date(2025, 0, 15, 12, 30, 45);
const result = setSecondsFromInput(base, 15);
// Returns: 2025-01-15 12:30:15
import { setSeconds } from "chronia";
const baseDate = new Date(2025, 0, 15, 12, 30, 45);
// Seconds roll over to next minute
const overflow = setSeconds(baseDate, 60);
// Returns: 2025-01-15 12:31:00
// Negative seconds roll back to previous minute
const underflow = setSeconds(baseDate, -1);
// Returns: 2025-01-15 12:29:59
// Large values cascade through time components
const largeValue = setSeconds(baseDate, 3600); // 60 minutes worth of seconds
// Returns: 2025-01-15 13:30:00
import { setSeconds } from "chronia";
const baseDate = new Date(2025, 0, 15, 12, 30, 45);
// Positive fractional values truncated toward zero
const fraction1 = setSeconds(baseDate, 30.9);
// Returns: 2025-01-15 12:30:30 (not 31)
// Negative fractional values truncated toward zero
const fraction2 = setSeconds(baseDate, -30.9);
// Returns: 2025-01-15 12:29:30 (not 29)
// Very small fractions become 0
const fraction3 = setSeconds(baseDate, 0.9);
// Returns: 2025-01-15 12:30:00
import { setSeconds } from "chronia";
// Invalid date input returns Invalid Date
const invalid1 = setSeconds(new Date("invalid"), 30);
// Returns: Invalid Date
// Invalid seconds input returns Invalid Date
const invalid2 = setSeconds(new Date(2025, 0, 15), NaN);
// Returns: Invalid Date
// Infinity returns Invalid Date
const invalid3 = setSeconds(new Date(2025, 0, 15), Infinity);
// Returns: Invalid Date
// Check for validity
function safeSetSeconds(date: Date, seconds: number): Date | null {
const result = setSeconds(date, seconds);
return isNaN(result.getTime()) ? null : result;
}