The diffMinutes function calculates the difference in complete minutes between two dates. It compares dates at the start of each minute, ignoring seconds and milliseconds for accurate minute counting.
function diffMinutes(dateLeft: DateInput, dateRight: DateInput): number;
| Parameter | Type | Description |
|---|---|---|
dateLeft |
DateInput |
The first date as a Date object, numeric timestamp, or ISO 8601 string |
dateRight |
DateInput |
The second date as a Date object, numeric timestamp, or ISO 8601 string |
| Type | Description |
|---|---|
number |
The difference in complete minutes between the two dates. Returns a positive number if dateLeft is after dateRight, negative if before, and NaN if any input is invalid |
The diffMinutes function computes the number of complete minutes between two dates by normalizing both dates to the start of their respective minutes (setting seconds and milliseconds to zero) and then calculating the time difference. This approach ensures that partial minutes are not counted, providing accurate minute-level precision for time difference calculations.
dateLeft represents a time that is after dateRightdateRight from dateLeftdateLeft represents a time that is before dateRightNaN when:dateLeft or dateRight is an Invalid Date object (e.g., new Date('invalid'))NaNInfinity-InfinityNaN for invalid inputs rather than throwing errors, following Chronia’s calculation function conventionsMath.round() to ensure integer results, eliminating floating-point precision issuesDate | number typesimport { diffMinutes } from "chronia";
// Track elapsed time
const sessionStart = new Date(2024, 5, 15, 14, 25);
const sessionEnd = new Date(2024, 5, 15, 14, 30);
const duration = diffMinutes(sessionEnd, sessionStart);
// Returns: 5
// Seconds are ignored (same minute)
const start = new Date(2024, 5, 15, 14, 30, 0);
const end = new Date(2024, 5, 15, 14, 30, 59);
const diff = diffMinutes(end, start);
// Returns: 0
// Works across hours and days
const yesterday = new Date(2024, 5, 14, 23, 50);
const today = new Date(2024, 5, 15, 0, 10);
const minutesPassed = diffMinutes(today, yesterday);
// Returns: 20
import { diffMinutes } from "chronia";
// Calculate time until an event
function getMinutesUntilEvent(eventTime: Date): number {
const now = new Date();
return diffMinutes(eventTime, now);
}
const meeting = new Date(2024, 5, 15, 15, 0);
const minutesRemaining = getMinutesUntilEvent(meeting);
if (minutesRemaining > 0) {
console.log(`Meeting starts in ${minutesRemaining} minutes`);
} else if (minutesRemaining === 0) {
console.log("Meeting is starting now");
} else {
console.log(`Meeting started ${Math.abs(minutesRemaining)} minutes ago`);
}
import { diffMinutes } from "chronia";
// Calculate meeting duration
interface Meeting {
startTime: number;
endTime: number;
}
function getMeetingDuration(meeting: Meeting): number {
return diffMinutes(meeting.endTime, meeting.startTime);
}
const meeting: Meeting = {
startTime: new Date(2024, 5, 15, 14, 0).getTime(),
endTime: new Date(2024, 5, 15, 15, 30).getTime(),
};
const duration = getMeetingDuration(meeting);
// Returns: 90
import { diffMinutes } from "chronia";
// Enforce minimum time between actions
function canPerformAction(
lastActionTime: Date,
cooldownMinutes: number,
): boolean {
const now = new Date();
const minutesSinceLastAction = diffMinutes(now, lastActionTime);
return minutesSinceLastAction >= cooldownMinutes;
}
const lastSubmit = new Date(2024, 5, 15, 14, 25);
const canSubmit = canPerformAction(lastSubmit, 5);
if (canSubmit) {
console.log("Action allowed");
} else {
const timeRemaining = 5 - diffMinutes(new Date(), lastSubmit);
console.log(`Please wait ${timeRemaining} more minutes`);
}
import { diffMinutes } from "chronia";
// Graceful error handling
function calculateSafeDifference(
date1: Date | number,
date2: Date | number,
): string {
const diff = diffMinutes(date1, date2);
if (isNaN(diff)) {
return "Invalid date input";
}
return `${Math.abs(diff)} minutes`;
}
// Valid inputs
calculateSafeDifference(
new Date(2024, 5, 15, 14, 30),
new Date(2024, 5, 15, 14, 25),
);
// Returns: '5 minutes'
// Invalid inputs return NaN, handled gracefully
calculateSafeDifference(new Date("invalid"), new Date(2024, 5, 15, 14, 25));
// Returns: 'Invalid date input'