hoursUntil

A utility function to calculate the number of hours remaining until a given date.
> hoursUntil(date: Date): number
Parameters
  • date: The future date to calculate the number of hours remaining until it.
  • Returns: The number of hours remaining until the given date. If the date is today or in the past, it returns `0`. If the date is invalid, it also returns `0`.

Example

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

// Calculate the number of hours until a future date
const futureDate = new Date("2025-01-01T10:00:00");
console.log(hoursUntil(futureDate)); // Number of hours until January 1st, 2025 at 10 AM

// Check with today's date
const today = new Date();
console.log(hoursUntil(today)); // 0

// Check with a past date
const pastDate = new Date("2023-01-01T10:00:00");
console.log(hoursUntil(pastDate)); // 0

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

Use Cases

  • Counting the remaining hours for a countdown event or deadline.
  • Calculating time intervals between a task's planned start time and the current time.
  • Setting reminders or notifications for upcoming events or due dates.