addSeconds

A utility function to add or subtract a specified number of seconds to/from a given date.
> addSeconds(seconds: number, date: Date = new Date()): Date
Parameters
  • seconds (number): The number of seconds to add. Use a negative number to subtract seconds.
  • date (Date, optional): The base date to modify. Defaults to the current date (new Date()).
  • Returns: A new Date object with the specified number of seconds added.

Example

import { addSeconds } from "@explita/daily-toolset";

// Add 45 seconds to the current time
const laterToday = addSeconds(45);
console.log(laterToday); // e.g., 2024-11-22T14:00:45.000Z

// Subtract 120 seconds (2 minutes) from a specific date
const earlierTime = addSeconds(-120, new Date("2024-11-22T14:00:00"));
console.log(earlierTime); // e.g., 2024-11-22T13:58:00.000Z

// Add 3600 seconds (1 hour) to a custom date
const customDate = new Date("2000-01-01T08:00:00");
const result = addSeconds(3600, customDate);
console.log(result); // e.g., 2000-01-01T09:00:00.000Z

Use Cases

  • Precise time adjustments for timestamp modifications.
  • Managing countdowns or timers requiring second-level granularity.
  • Handling dynamic intervals for time-sensitive applications.