UNPKG

2.1 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Jupyter Development Team.
3// Distributed under the terms of the Modified BSD License.
4Object.defineProperty(exports, "__esModule", { value: true });
5exports.Time = void 0;
6/**
7 * A list of time units with their associated value in milliseconds.
8 */
9const UNITS = [
10 { name: 'years', milliseconds: 365 * 24 * 60 * 60 * 1000 },
11 { name: 'months', milliseconds: 30 * 24 * 60 * 60 * 1000 },
12 { name: 'days', milliseconds: 24 * 60 * 60 * 1000 },
13 { name: 'hours', milliseconds: 60 * 60 * 1000 },
14 { name: 'minutes', milliseconds: 60 * 1000 },
15 { name: 'seconds', milliseconds: 1000 }
16];
17/**
18 * The namespace for date functions.
19 */
20var Time;
21(function (Time) {
22 /**
23 * Convert a timestring to a human readable string (e.g. 'two minutes ago').
24 *
25 * @param value - The date timestring or date object.
26 *
27 * @returns A formatted date.
28 */
29 function formatHuman(value, format = 'long') {
30 const lang = document.documentElement.lang || 'en';
31 const formatter = new Intl.RelativeTimeFormat(lang, {
32 numeric: 'auto',
33 style: format
34 });
35 const delta = new Date(value).getTime() - Date.now();
36 for (let unit of UNITS) {
37 const amount = Math.ceil(delta / unit.milliseconds);
38 if (amount === 0) {
39 continue;
40 }
41 return formatter.format(amount, unit.name);
42 }
43 return formatter.format(0, 'seconds');
44 }
45 Time.formatHuman = formatHuman;
46 /**
47 * Convenient helper to convert a timestring to a date format.
48 *
49 * @param value - The date timestring or date object.
50 *
51 * @returns A formatted date.
52 */
53 function format(value) {
54 const lang = document.documentElement.lang || 'en';
55 const formatter = new Intl.DateTimeFormat(lang, {
56 dateStyle: 'short',
57 timeStyle: 'short'
58 });
59 return formatter.format(new Date(value));
60 }
61 Time.format = format;
62})(Time || (exports.Time = Time = {}));
63//# sourceMappingURL=time.js.map
\No newline at end of file