@jupyterlab/coreutils
Version:
JupyterLab - Core Utilities
63 lines • 2.1 kB
JavaScript
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.Time = void 0;
/**
* A list of time units with their associated value in milliseconds.
*/
const UNITS = [
{ name: 'years', milliseconds: 365 * 24 * 60 * 60 * 1000 },
{ name: 'months', milliseconds: 30 * 24 * 60 * 60 * 1000 },
{ name: 'days', milliseconds: 24 * 60 * 60 * 1000 },
{ name: 'hours', milliseconds: 60 * 60 * 1000 },
{ name: 'minutes', milliseconds: 60 * 1000 },
{ name: 'seconds', milliseconds: 1000 }
];
/**
* The namespace for date functions.
*/
var Time;
(function (Time) {
/**
* Convert a timestring to a human readable string (e.g. 'two minutes ago').
*
* @param value - The date timestring or date object.
*
* @returns A formatted date.
*/
function formatHuman(value, format = 'long') {
const lang = document.documentElement.lang || 'en';
const formatter = new Intl.RelativeTimeFormat(lang, {
numeric: 'auto',
style: format
});
const delta = new Date(value).getTime() - Date.now();
for (let unit of UNITS) {
const amount = Math.ceil(delta / unit.milliseconds);
if (amount === 0) {
continue;
}
return formatter.format(amount, unit.name);
}
return formatter.format(0, 'seconds');
}
Time.formatHuman = formatHuman;
/**
* Convenient helper to convert a timestring to a date format.
*
* @param value - The date timestring or date object.
*
* @returns A formatted date.
*/
function format(value) {
const lang = document.documentElement.lang || 'en';
const formatter = new Intl.DateTimeFormat(lang, {
dateStyle: 'short',
timeStyle: 'short'
});
return formatter.format(new Date(value));
}
Time.format = format;
})(Time || (exports.Time = Time = {}));
//# sourceMappingURL=time.js.map
;