fix "ending in 0m" time bug
This commit is contained in:
parent
da8e62cbc1
commit
ff60529b3f
1 changed files with 13 additions and 16 deletions
|
|
@ -174,24 +174,21 @@ export function formatTimeRemaining(timeMs: number): string {
|
||||||
return `${minutes}m`;
|
return `${minutes}m`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatTimeUntil(dateString: string): string {
|
export function formatTimeUntil(date: Date | string | number): string {
|
||||||
const now = new Date();
|
const now = Date.now();
|
||||||
const target = new Date(dateString);
|
const target = typeof date === 'number'
|
||||||
const diffMs = target.getTime() - now.getTime();
|
? date * (date < 1e12 ? 1000 : 1)
|
||||||
|
: new Date(date).getTime();
|
||||||
|
let diff = Math.max(0, target - now);
|
||||||
|
|
||||||
if (diffMs <= 0) return 'Ended';
|
const minutes = Math.floor(diff / 60000);
|
||||||
|
const hours = Math.floor(diff / 3600000);
|
||||||
|
const days = Math.floor(diff / 86400000);
|
||||||
|
|
||||||
const days = Math.floor(diffMs / (24 * 60 * 60 * 1000));
|
if (diff < 60000) return 'less than 1m';
|
||||||
const hours = Math.floor((diffMs % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000));
|
if (minutes < 60) return `${minutes}m`;
|
||||||
const minutes = Math.floor((diffMs % (60 * 60 * 1000)) / (60 * 1000));
|
if (hours < 24) return `${hours}h`;
|
||||||
|
return `${days}d`;
|
||||||
if (days > 0) {
|
|
||||||
return `${days}d ${hours}h`;
|
|
||||||
} else if (hours > 0) {
|
|
||||||
return `${hours}h ${minutes}m`;
|
|
||||||
} else {
|
|
||||||
return `${minutes}m`;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getExpirationDate(option: string): string | null {
|
export function getExpirationDate(option: string): string | null {
|
||||||
|
|
|
||||||
Reference in a new issue