A library to calculate the time difference between the current date and a provided date.
npm install date-and-time-difference-calculatorA simple JavaScript utility provided with a Node.JS package to calculate the time difference between the current date and a given future target date. It returns the difference in days, hours, and minutes, and handles invalid or past dates.
Calculates the difference between the current date and a specified future target date.
Returns the result in days, hours, and minutes.
Handles invalid date inputs and dates in the past.
Installation
You can use this function in your project by either downloading the code or using it as a module.
Alternatively, if you're using a Node.js environment, you can install it directly via npm or import it into your JavaScript project.
Usage
Import the function
``javascript`
const { calculateTimeDifference } = require("./path-to-file"); // Adjust path as needed
`javascript
const targetDate = "2025-12-31T23:59:59"; // Format: "YYYY-MM-DDTHH:mm:ss"
const timeDifference = calculateTimeDifference(targetDate);
console.log(timeDifference);
// Output: { days: 123, hours: 4, minutes: 30 }
`
If the provided target date is in the past, the function will return an error message.
`javascript
const targetDate = "2020-01-01T00:00:00";
const timeDifference = calculateTimeDifference(targetDate);
console.log(timeDifference);
// Output: { error: "The date provided is in the past!" }
`
If the provided date is invalid, an error is thrown:
`javascript``
const targetDate = "Invalid Date!";
try {
const timeDifference = calculateTimeDifference(targetDate);
} catch (error) {
console.error(error.message);
// Output: "Invalid date format!"
}
calculateTimeDifference(targetDate)
Parameters:
targetDate: A string representing the future date in ISO 8601 format (YYYY-MM-DDTHH:mm:ss).
Returns:
An object with days, hours, and minutes properties representing the time difference, or an error message if the date is in the past or invalid.
Throws:
An error if the provided targetDate is not a valid date format.
Eder TS
MIT License. See the LICENSE file for more details.