29 lines
No EOL
893 B
TypeScript
29 lines
No EOL
893 B
TypeScript
export const currentDaytime = function(): string {
|
|
const currentHour = new Date().getHours()
|
|
|
|
if (currentHour >= 0 && currentHour < 12) {
|
|
return 'morning'
|
|
} else if (currentHour >= 12 && currentHour < 18) {
|
|
return 'day'
|
|
}
|
|
return 'evening'
|
|
}
|
|
|
|
export function relativeTimeFromDate(date: Date): {val: number, range: Intl.RelativeTimeFormatUnit} | undefined {
|
|
const units: {unit: Intl.RelativeTimeFormatUnit; ms: number}[] = [
|
|
{unit: "year", ms: 31536000000},
|
|
{unit: "month", ms: 2628000000},
|
|
{unit: "day", ms: 86400000},
|
|
{unit: "hour", ms: 3600000},
|
|
{unit: "minute", ms: 60000},
|
|
{unit: "second", ms: 1000},
|
|
];
|
|
|
|
const elapsed = date.getTime() - new Date().getTime();
|
|
for (const {unit, ms} of units) {
|
|
if (Math.abs(elapsed) >= ms || unit === "second") {
|
|
return {val: Math.round(elapsed / ms), range: unit};
|
|
}
|
|
}
|
|
return undefined;
|
|
} |