1.0.0 • Published 5 months ago

date-and-time-difference-calculator v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

Time Difference Calculator

A 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.

Features

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.

Download the file or clone the repository.

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

const { calculateTimeDifference } = require("./path-to-file"); // Adjust path as needed

Example

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 }

Error Handling

If the provided target date is in the past, the function will return an error message.

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:

const targetDate = "Invalid Date!";
try {
  const timeDifference = calculateTimeDifference(targetDate);
} catch (error) {
  console.error(error.message);
  // Output: "Invalid date format!"
}

Function Explanation

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.

Author

Eder TS

License

MIT License. See the LICENSE file for more details.

1.0.0

5 months ago