addHours
A utility function to add or subtract a specified number of hours to/from a given date.> addHours(hours: number, date: Date = new Date()): Date
- hours (number): The number of hours to add. Use a negative number to subtract hours.
- 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 hours added.
Example
import { addHours } from "@explita/daily-toolset";
// Add 5 hours to the current time
const laterToday = addHours(5);
console.log(laterToday); // e.g., 2024-11-22T18:00:00.000Z
// Subtract 3 hours from a specific date
const earlierTime = addHours(-3, new Date("2024-11-22T12:00:00"));
console.log(earlierTime); // e.g., 2024-11-22T09:00:00.000Z
// Add 10 hours to a custom date
const customDate = new Date("2000-01-01T08:00:00");
const result = addHours(10, customDate);
console.log(result); // e.g., 2000-01-01T18:00:00.000Z
Use Cases
- Adjusting timestamps for time zone differences or daylight savings.
- Scheduling or calculating event times based on shifts in hours.
- Working with time-sensitive data in applications like reminders or scheduling tools.