Skip to content

Instantly share code, notes, and snippets.

@xmsz
Created January 22, 2021 02:32
Show Gist options
  • Save xmsz/76fb2fa097d12d931a71467a161198ed to your computer and use it in GitHub Desktop.
Save xmsz/76fb2fa097d12d931a71467a161198ed to your computer and use it in GitHub Desktop.
工具包-获取倒计时信息
// CANDO 1. MaxUnit
const DAY = 24 * 60 * 60 * 1000;
const HOUR = 60 * 60 * 1000;
const MIN = 60 * 1000;
function timeZero(time: number) {
return time < 10 ? `0${time}` : String(time);
}
interface ICountdownResult {
day: string;
hour: string;
min: string;
sec: string;
mesc: string;
isEnd: boolean;
}
function getRemainingTimeInfo(remainingTime: number) {
const result: ICountdownResult = {
day: '00',
hour: '00',
min: '00',
sec: '00',
mesc: '00',
isEnd: remainingTime <= 0,
};
const d = Math.floor(remainingTime / DAY);
const lastHour = remainingTime - d * DAY;
const h = Math.floor(lastHour / HOUR);
const lastMin = lastHour - h * HOUR;
const m = Math.floor(lastMin / MIN);
const s = Math.floor((lastMin - m * MIN) / 1000);
const mesc = Math.floor(remainingTime % 1000);
result.day = timeZero(d);
if (h > 0) {
result.hour = timeZero(h);
}
if (m < 60) {
result.min = timeZero(m);
}
result.sec = timeZero(s);
result.mesc = String(mesc);
if (result.mesc.length !== 3) {
result.mesc = `00${result.mesc}`;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment