Skip to content

Instantly share code, notes, and snippets.

@hebertcisco
Last active March 25, 2022 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hebertcisco/108d7b914e2b65ded06effa7ba31788c to your computer and use it in GitHub Desktop.
Save hebertcisco/108d7b914e2b65ded06effa7ba31788c to your computer and use it in GitHub Desktop.
type TypeGetTimeRemaining = {
total: number;
days: number;
hours: number;
minutes: number;
seconds: number;
};
function getTimeRemaining(endtime: string): TypeGetTimeRemaining {
let time = Date.parse(endtime) - Date.parse(new Date().toDateString());
const THOUSAND = 1000;
const HOURS_IN_DAY = 24;
const MINUTES_IN_HOUR = 60;
const SECONDS_IN_MINUTE = MINUTES_IN_HOUR;
let seconds = Math.floor(time / THOUSAND % SECONDS_IN_MINUTE);
let minutes = Math.floor(
time / THOUSAND / SECONDS_IN_MINUTE % MINUTES_IN_HOUR
);
let hours = Math.floor(
time / (THOUSAND * SECONDS_IN_MINUTE * MINUTES_IN_HOUR) % HOURS_IN_DAY
);
let days = Math.floor(
time / (THOUSAND * SECONDS_IN_MINUTE * MINUTES_IN_HOUR * HOURS_IN_DAY)
);
return {
total: time,
days: days,
hours: hours,
minutes: minutes,
seconds: seconds
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment