Second

startOfSecond

Returns the timestamp at the start of the second (milliseconds set to 0).

reference/second/startOfSecond.ts
import { startOfSecond, toISOString } from "datezone";

const ts = 1700000000999; // 2023-11-14T22:13:20.999Z

// Get start of second
const start = startOfSecond(ts);
console.log(start, toISOString(start, "UTC")); // 1700000000000 (2023-11-14T22:13:20.000Z)

endOfSecond

Returns the timestamp at the end of the second (999 milliseconds).

reference/second/endOfSecond.ts
import { endOfSecond, toISOString } from "datezone";

const ts = 1700000000000; // 2023-11-14T22:13:20.000Z

// Get end of second
const end = endOfSecond(ts);
console.log(end, toISOString(end, "UTC")); // 1700000000999 (2023-11-14T22:13:20.999Z)

addSeconds

Adds a specified number of seconds to a timestamp.

reference/second/addSeconds.ts
import { addSeconds, toISOString } from "datezone";

const ts = 1700000000000; // 2023-11-14T22:13:20.000Z

// Add positive seconds
const inThirtySeconds = addSeconds(ts, 30);
console.log(inThirtySeconds, toISOString(inThirtySeconds, "UTC")); // 1700000030000 (2023-11-14T22:13:50.000Z)

// Add negative seconds (subtract)
const thirtySecondsAgo = addSeconds(ts, -30);
console.log(thirtySecondsAgo, toISOString(thirtySecondsAgo, "UTC")); // 1699999970000 (2023-11-14T22:12:50.000Z)

// Add many seconds
const inOneHour = addSeconds(ts, 3600);
console.log(inOneHour, toISOString(inOneHour, "UTC")); // 1700003600000 (2023-11-14T23:13:20.000Z)

// Example with specific timestamp
const specificTime = new Date("2023-12-25T10:00:00Z").getTime();
const plus45 = addSeconds(specificTime, 45);
const minus120 = addSeconds(specificTime, -120);

console.log(plus45, toISOString(plus45, "UTC")); // 1703498445000 (2023-12-25T10:00:45.000Z)
console.log(minus120, toISOString(minus120, "UTC")); // 1703498280000 (2023-12-25T09:58:00.000Z)

subSeconds

Subtracts a specified number of seconds from a timestamp.

reference/second/subSeconds.ts
import { subSeconds, toISOString } from "datezone";

const ts = 1700000000000; // 2023-11-14T22:13:20.000Z

// Subtract positive seconds
const fortyFiveSecondsAgo = subSeconds(ts, 45);
console.log(fortyFiveSecondsAgo, toISOString(fortyFiveSecondsAgo, "UTC")); // 1699999955000 (2023-11-14T22:12:35.000Z)

// Subtract negative seconds (adds)
const inSixtySeconds = subSeconds(ts, -60);
console.log(inSixtySeconds, toISOString(inSixtySeconds, "UTC")); // 1700000060000 (2023-11-14T22:14:20.000Z)

// Subtract many seconds
const oneHourAgo = subSeconds(ts, 3600);
console.log(oneHourAgo, toISOString(oneHourAgo, "UTC")); // 1699996400000 (2023-11-14T21:13:20.000Z)

// Example with specific timestamp
const specificTime = new Date("2023-12-25T15:30:00Z").getTime();
const minus15 = subSeconds(specificTime, 15);
const minus300 = subSeconds(specificTime, 300);

console.log(minus15, toISOString(minus15, "UTC")); // 1703518195000 (2023-12-25T15:29:45.000Z)
console.log(minus300, toISOString(minus300, "UTC")); // 1703517910000 (2023-12-25T15:25:10.000Z)

addMilliseconds

Adds a specified number of milliseconds to a timestamp. This is the most precise time addition operation available.

reference/second/addMilliseconds.ts
import { addMilliseconds, toISOString } from "datezone";

const ts = 1700000000000; // 2023-11-14T22:13:20.000Z

// Add positive milliseconds
const plus500 = addMilliseconds(ts, 500);
console.log(plus500, toISOString(plus500, "UTC")); // 1700000500000 (2023-11-14T22:13:20.500Z)

// Add negative milliseconds
const minus500 = addMilliseconds(ts, -500);
console.log(minus500, toISOString(minus500, "UTC")); // 1699999500000 (2023-11-14T22:13:19.500Z)

subMilliseconds

Subtracts a specified number of milliseconds from a timestamp. This is the most precise time subtraction operation available.

reference/second/subMilliseconds.ts
import { subMilliseconds, toISOString } from "datezone";

const ts = 1700000000000; // 2023-11-14T22:13:20.000Z

// Subtract positive milliseconds
const minus300 = subMilliseconds(ts, 300);
console.log(minus300, toISOString(minus300, "UTC")); // 1699999999700 (2023-11-14T22:13:19.700Z)

// Subtract negative milliseconds (adds)
const plus700 = subMilliseconds(ts, -700);
console.log(plus700, toISOString(plus700, "UTC")); // 1700000000700 (2023-11-14T22:13:20.700Z)

second

Second and millisecond manipulation utilities for date and time values.

reference/second/second.ts
import { second } from "datezone";

const timestamp = Date.UTC(2024, 0, 1, 12, 10, 15);
console.log("Second", second(timestamp)); // 15

On this page