wesleysladis-date-difference
v1.0.0
Published
A Library to calculate the time difference between the current date and a provided date.
Maintainers
Readme
date-difference-calculator
A small Node.js library for calculating the time difference from the current date to a target date in days, hours, and minutes.
Installation
npm install date-difference-calculatorUsage
const { calculateTimeDifference } = require('date-difference-calculator');
const targetDate = new Date('2025-01-03T12:30:45Z');
const diff = calculateTimeDifference(targetDate);
console.log(diff);Output
The result is an object with the following shape:
days- number of full dayshours- remaining hours after daysminutes- remaining minutes after hours
Or, if the target date is in the past:
{
"error": "The date provided is in the past"
}Example result (assuming current date is before the target):
{
"days": 2,
"hours": 12,
"minutes": 30
}API
calculateTimeDifference(targetDate)
targetDate- aDateobject or valid date string
Returns an object describing the time difference to the target date, or an error if the date is in the past.
Throws an error if the date format is invalid.
Notes
- The function uses the current date and time for comparison.
- If
targetDateis in the past, returns an error object. - Invalid date formats will throw an error.
