minutesBetween
A utility function to calculate the number of minutes between two given dates.> minutesBetween(date1: Date, date2: Date): number
- date1: The first date to compare.
- date2: The second date to compare.
- Returns: The number of minutes between the two dates as a positive integer. If either of the dates is invalid, it returns `0`.
Example
import { minutesBetween } from "@explita/daily-toolset";
// Calculate the number of minutes between two dates
const date1 = new Date("2023-01-01T10:00:00");
const date2 = new Date("2023-01-01T12:30:00");
console.log(minutesBetween(date1, date2)); // 150
// Check with the same date
const sameDate = new Date("2023-01-01T10:00:00");
console.log(minutesBetween(sameDate, sameDate)); // 0
// Check with an invalid date
const invalidDate = new Date("invalid date string");
console.log(minutesBetween(date1, invalidDate)); // 0
Use Cases
- Calculating the time difference between two events in minutes.
- Tracking elapsed time between tasks or steps in a process.
- Determining the number of minutes remaining before a deadline or event.