83 lines
2.0 KiB
TypeScript
83 lines
2.0 KiB
TypeScript
export class Age
|
|
{
|
|
static calc(date: Date)
|
|
{
|
|
const today = new Date().getTime() / 1000;
|
|
const createdAt = new Date(date).getTime() / 1000;
|
|
const dif = today - createdAt;
|
|
if (dif < 60)
|
|
{
|
|
return Age.format(dif, ChronoUnit.SECOND) + " ago";
|
|
}
|
|
if (dif < 60 * 60)
|
|
{
|
|
const minutes = Math.floor((dif / 60));
|
|
return Age.format(minutes, ChronoUnit.MINUTE) + " ago";
|
|
}
|
|
if (dif < 60 * 60 * 24)
|
|
{
|
|
const hours = Math.floor(dif / (60 * 60));
|
|
return Age.format(hours, ChronoUnit.HOUR) + " ago";
|
|
}
|
|
else
|
|
{
|
|
const days = Math.floor(dif / (60 * 60 * 24));
|
|
return Age.format(days, ChronoUnit.DAY) + " ago";
|
|
}
|
|
}
|
|
|
|
static format(count: number, unit: ChronoUnit): string
|
|
{
|
|
if (unit === ChronoUnit.SECOND)
|
|
{
|
|
if (count != 1)
|
|
{
|
|
return count.toFixed(0) + " seconds";
|
|
}
|
|
else
|
|
{
|
|
return count.toFixed(0) + " second";
|
|
}
|
|
}
|
|
if (unit === ChronoUnit.MINUTE)
|
|
{
|
|
if (count != 1)
|
|
{
|
|
return count.toFixed(0) + " minutes";
|
|
}
|
|
else
|
|
{
|
|
return count.toFixed(0) + " minute";
|
|
}
|
|
}
|
|
if (unit === ChronoUnit.HOUR)
|
|
{
|
|
if (count != 1)
|
|
{
|
|
return count.toFixed(0) + " hours";
|
|
}
|
|
else
|
|
{
|
|
return count.toFixed(0) + " hour";
|
|
}
|
|
}
|
|
if (unit === ChronoUnit.DAY)
|
|
{
|
|
if (count != 1)
|
|
{
|
|
return count.toFixed(0) + " days";
|
|
}
|
|
else
|
|
{
|
|
return count.toFixed(0) + " day";
|
|
}
|
|
}
|
|
return "-";
|
|
}
|
|
}
|
|
|
|
|
|
enum ChronoUnit
|
|
{
|
|
SECOND, MINUTE, HOUR, DAY
|
|
} |