date-diff-kauan
Version:
A library to calculate the difference between dates
26 lines (21 loc) • 721 B
JavaScript
function calculateTimeDifference(targetDate) {
const currentDate = new Date();
const futereDate = new Date(targetDate);
if (isNaN(targetDate)) {
throw new Error('Invalid date format');
}
if (futereDate <= currentDate) {
return {error: 'The date provided is in the past'};
}
const diffInMillis = futereDate - currentDate;
const diffInMinutes = Math.floor(diffInMillis / (1000 * 60));
const days = Math.floor(diffInMinutes / (60 * 24));
const hours = Math.floor((diffInMinutes % (60 * 24)) / 60);
const minutes = Math.floor(diffInMinutes % 60);
return {
days,
hours,
minutes,
}
}
module.exports = { calculateTimeDifference };