UNPKG

date-difference-calculate

Version:

A library to calcute the times difference between teh current date and a provided date

26 lines (21 loc) 732 B
function calculateTimeDifference (targetDate){ const currenceDate = new Date(); const futureDate = new Date(targetDate); if (isNaN(targetDate)){ throw new Error('invalid date format') } if (futureDate <= currenceDate){ return{error: "the date provide is in the past"} } const diffInMillis = futureDate - currenceDate const diffInMinuts = Math.floor(diffInMillis / (1000 * 60)) const days = Math.floor(diffInMinuts / (60 * 24)) const hours = Math.floor(diffInMinuts % (60 * 24) / 60) const minutes = Math.floor(diffInMinuts % 60 ) return{ days, hours, minutes, }; } module.exports = { calculateTimeDifference };