The base date as a Date object or timestamp (number)
A new Date object truncated to the start of the second, or Invalid Date if input is invalid
// Basic truncation
const result = truncSecond(new Date(2024, 5, 15, 14, 30, 45, 123));
// Returns: June 15, 2024 14:30:45.000
// Already at start of second
const result2 = truncSecond(new Date(2024, 5, 15, 14, 30, 45, 0));
// Returns: June 15, 2024 14:30:45.000 (unchanged)
// Works with timestamps
const timestamp = Date.now();
const result3 = truncSecond(timestamp);
// Returns: Date at XX:XX:XX.000 of current second
// End of second
const result4 = truncSecond(new Date(2024, 5, 15, 14, 30, 45, 999));
// Returns: June 15, 2024 14:30:45.000
// Invalid inputs return Invalid Date
const result5 = truncSecond(new Date("invalid"));
// Returns: Invalid Date
Truncate a date to the start of the second.
This function sets the milliseconds to 0 while keeping the same date, hour, minute, and second, effectively removing all time components below the second level.