The base date as a Date object or timestamp (number)
The timestamp in milliseconds since Unix epoch (January 1, 1970, 00:00:00 UTC)
A new Date object with the timestamp set, or Invalid Date if any input is invalid
// Set to a specific timestamp (Date input)
const result = setTime(new Date(), 1704067200000);
// Returns: 2024-01-01T00:00:00.000Z
// Set to a specific timestamp (number input)
const result2 = setTime(1609459200000, 1704067200000);
// Returns: 2024-01-01T00:00:00.000Z
// Set to Unix epoch
const result3 = setTime(new Date(), 0);
// Returns: 1970-01-01T00:00:00.000Z
// Set to negative timestamp (before epoch)
const result4 = setTime(new Date(), -86400000);
// Returns: 1969-12-31T00:00:00.000Z
// Fractional milliseconds are preserved
const result5 = setTime(new Date(), 1.5);
// Returns: timestamp with 1.5 milliseconds (truncated by Date API)
// Invalid timestamp returns Invalid Date
const result6 = setTime(new Date(), NaN);
// Returns: Invalid Date
Set the complete timestamp of the given date.
This function validates arguments before processing and returns a new Date instance with the specified timestamp. Unlike other setters that modify components (year, month, etc.), setTime replaces the entire timestamp value.