MCPcopy Create free account
hub / github.com/DeepNotesApp/DeepNotes / relativeTimeStr

Function relativeTimeStr

packages/@stdlib/misc/src/datetime.ts:41–75  ·  view source on GitHub ↗
(
  dateDiff?: number,
  locales: string | string[] = 'en',
  options?: Intl.RelativeTimeFormatOptions,
)

Source from the content-addressed store, hash-verified

39}
40
41export function relativeTimeStr(
42 dateDiff?: number,
43 locales: string | string[] = 'en',
44 options?: Intl.RelativeTimeFormatOptions,
45): string {
46 if (dateDiff == null) {
47 return '';
48 }
49
50 const absDateDiff = Math.abs(dateDiff);
51
52 const rtf = new Intl.RelativeTimeFormat(locales, options);
53
54 if (absDateDiff < 1000) {
55 return 'now';
56 } else if (absDateDiff < 1000 * 60) {
57 return rtf.format(Math.round(dateDiff / 1000), 'second');
58 } else if (absDateDiff < 1000 * 60 * 60) {
59 return rtf.format(Math.round(dateDiff / (1000 * 60)), 'minute');
60 } else if (absDateDiff < 1000 * 60 * 60 * 24) {
61 return rtf.format(Math.round(dateDiff / (1000 * 60 * 60)), 'hour');
62 } else if (absDateDiff < 1000 * 60 * 60 * 24 * 30) {
63 return rtf.format(Math.round(dateDiff / (1000 * 60 * 60 * 24)), 'day');
64 } else if (absDateDiff < 1000 * 60 * 60 * 24 * 30 * 12) {
65 return rtf.format(
66 Math.round(dateDiff / (1000 * 60 * 60 * 24 * 30)),
67 'month',
68 );
69 } else {
70 return rtf.format(
71 Math.round(dateDiff / (1000 * 60 * 60 * 24 * 30 * 12)),
72 'year',
73 );
74 }
75}

Callers

nothing calls this directly

Calls 2

absMethod · 0.80
formatMethod · 0.80

Tested by

no test coverage detected