function getTimeRemaining(endtime) {
const total = Date.parse(endtime) - Date.now();
const seconds = Math.floor((total / 1000) % 60);
const minutes = Math.floor((total / 1000 / 60) % 60);
const hours = Math.floor((total / (1000 * 60 * 60)) % 24);
const days = Math.floor(total / (1000 * 60 * 60 * 24));
return {
total,
days,
hours,
minutes,
seconds
};
}
Date.parse(endtime)
converts the end time into a timestamp (in milliseconds).Date.now()
gets the current time in milliseconds.- We calculate the difference (
total
), then use that to extract the days, hours, minutes, and seconds. - The function returns an object containing the remaining time.