61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
export const getTimeRemaining = (endtime) => {
|
|
const t = Date.parse(endtime) - Date.parse(new Date().toISOString())
|
|
return {
|
|
'total': t,
|
|
'days': Math.floor(t / (1000 * 60 * 60 * 24)),
|
|
'hours': Math.floor((t / (1000 * 60 * 60)) % 24),
|
|
'minutes': Math.floor((t / 1000 / 60) % 60),
|
|
'seconds': Math.floor((t / 1000) % 60)
|
|
};
|
|
}
|
|
|
|
export const timeSince = (date: Date) => {
|
|
|
|
const seconds = Math.floor((Date.now() - date.getTime()) / 1000);
|
|
let interval = seconds / 31536000;
|
|
let intervalType: string;
|
|
|
|
if (interval >= 1) {
|
|
intervalType = 'year';
|
|
} else {
|
|
interval = Math.floor(seconds / 2592000);
|
|
if (interval >= 1) {
|
|
intervalType = 'month';
|
|
} else {
|
|
interval = Math.floor(seconds / 86400);
|
|
if (interval >= 1) {
|
|
intervalType = 'day';
|
|
} else {
|
|
interval = Math.floor(seconds / 3600);
|
|
if (interval >= 1) {
|
|
intervalType = "hour";
|
|
} else {
|
|
interval = Math.floor(seconds / 60);
|
|
if (interval >= 1) {
|
|
intervalType = "minute";
|
|
} else {
|
|
interval = seconds;
|
|
intervalType = "second";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (interval > 1 || interval === 0) {
|
|
intervalType += 's';
|
|
}
|
|
|
|
return interval + ' ' + intervalType;
|
|
}
|
|
|
|
export const wait = (ms: number) => {
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
}
|
|
|
|
export const closeTo = (date1: Date, date2: Date, tolerance: number) => {
|
|
const a = date1.getTime()
|
|
const b = date2.getTime()
|
|
return Math.abs(a - b) < tolerance
|
|
}
|