flamenco/web/app/src/datetime.js
Sybren A. Stüvel 40bed3db5e Web: add notification history pop-over
Add a notification pop-over, which can be opened by clicking the footer
bar.
2022-05-19 12:57:00 +02:00

45 lines
1.4 KiB
JavaScript

import { DateTime } from "luxon";
const relativeTimeDefaultOptions = {
thresholdDays: 14,
format: DateTime.DATE_MED_WITH_WEEKDAY,
}
/**
* Convert the given timestamp to a Luxon time object.
*
* @param {Date | string} timestamp either a Date object or an ISO time string.
* @returns Luxon time object.
*/
function parseTimestamp(timestamp) {
if (timestamp instanceof Date) {
return DateTime.fromJSDate(timestamp);
}
return DateTime.fromISO(timestamp);
}
// relativeTime parses the timestamp (can be ISO-formatted string or JS Date
// object) and returns it in string form. The returned string is either "xxx
// time ago" if it's a relatively short time ago, or the formatted absolute time
// otherwise.
export function relativeTime(timestamp, options) {
const parsedTimestamp = parseTimestamp(timestamp);
if (!options) options = relativeTimeDefaultOptions;
const now = DateTime.local();
const ageInDays = now.diff(parsedTimestamp).as('days');
if (ageInDays > options.format)
return parsedTimestamp.toLocaleString(options.format);
return parsedTimestamp.toRelative({style: "narrow"});
}
export function shortened(timestamp) {
const parsedTimestamp = parseTimestamp(timestamp);
const now = DateTime.local();
const ageInHours = now.diff(parsedTimestamp).as('hours');
if (ageInHours < 24)
return parsedTimestamp.toLocaleString(DateTime.TIME_24_SIMPLE);
return parsedTimestamp.toLocaleString(DateTime.DATE_MED_WITH_WEEKDAY);
}