hoursBetween

A utility function to calculate the number of hours between two given dates.
> hoursBetween(date1: Date, date2: Date): number
Parameters
  • date1: The first date to compare.
  • date2: The second date to compare.
  • Returns: The number of hours between the two dates as a positive integer. If either of the dates is invalid, it returns `0`.

Example

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

// Calculate the number of hours between two dates
const date1 = new Date("2023-01-01T10:00:00");
const date2 = new Date("2023-01-02T08:00:00");
console.log(hoursBetween(date1, date2)); // 22

// Check with the same date
const sameDate = new Date("2023-01-01T10:00:00");
console.log(hoursBetween(sameDate, sameDate)); // 0

// Check with an invalid date
const invalidDate = new Date("invalid date string");
console.log(hoursBetween(date1, invalidDate)); // 0

Use Cases

  • Calculating the time difference between two events in hours.
  • Tracking elapsed hours between tasks or milestones.
  • Determining the number of hours until a specific deadline or event.