memories/src/services/Utils.ts

28 lines
903 B
TypeScript
Raw Normal View History

2022-09-16 23:17:45 +00:00
import { getCanonicalLocale } from "@nextcloud/l10n";
2022-09-12 19:15:28 +00:00
/** Get JS date object from dayId */
2022-09-13 01:33:24 +00:00
export function dayIdToDate(dayId: number){
return new Date(dayId*86400*1000);
2022-09-12 19:15:28 +00:00
}
/** Get month name from number */
2022-09-16 23:17:45 +00:00
export function getShortDateStr(date: Date) {
return date.toLocaleDateString(getCanonicalLocale(), {
2022-09-12 19:15:28 +00:00
month: 'short',
2022-09-16 23:17:45 +00:00
year: 'numeric',
timeZone: 'UTC',
});
}
/** Get long date string with optional year if same as current */
2022-09-25 13:21:40 +00:00
export function getLongDateStr(date: Date, skipYear=false, time=false) {
2022-09-16 23:17:45 +00:00
return date.toLocaleDateString(getCanonicalLocale(), {
weekday: 'long',
month: 'long',
day: 'numeric',
year: (skipYear && date.getUTCFullYear() === new Date().getUTCFullYear()) ? undefined : 'numeric',
2022-09-12 19:15:28 +00:00
timeZone: 'UTC',
2022-09-25 13:21:40 +00:00
hour: time ? 'numeric' : undefined,
minute: time ? 'numeric' : undefined,
2022-09-12 19:15:28 +00:00
});
}