flamenco/web/app/src/datetime.js
Sybren A. Stüvel 316ba6953b Web: show job details column
This may be a nice moment to reconsider using Pinia as a data store, as
we now have two views (job table + job details) that should share a data
set.
2022-04-12 15:28:18 +02:00

28 lines
903 B
JavaScript

import { DateTime } from "luxon";
const relativeTimeDefaultOptions = {
thresholdDays: 14,
format: DateTime.DATE_MED_WITH_WEEKDAY,
}
// 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) {
let parsedTimestamp = null;
if (timestamp instanceof Date) {
parsedTimestamp = DateTime.fromJSDate(timestamp);
} else {
parsedTimestamp = DateTime.fromISO(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();
}