Skip to content

Commit 0b23c15

Browse files
committed
Changes relative dates for < 1yr to be months
Refs: #1546
1 parent 689a2e9 commit 0b23c15

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/system/date.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
const customDateTimeFormatParserRegex =
33
/(?<literal>\[.*?\])|(?<year>YYYY|YY)|(?<month>M{1,4})|(?<day>Do|DD?)|(?<weekday>d{2,4})|(?<hour>HH?|hh?)|(?<minute>mm?)|(?<second>ss?)|(?<fractionalSecond>SSS)|(?<dayPeriod>A|a)|(?<timeZoneName>ZZ?)/g;
44
const dateTimeFormatRegex = /(?<dateStyle>full|long|medium|short)(?:\+(?<timeStyle>full|long|medium|short))?/;
5-
const relativeUnitThresholds: [Intl.RelativeTimeFormatUnit, number, string][] = [
6-
['year', 24 * 60 * 60 * 1000 * 365, 'yr'],
7-
['month', (24 * 60 * 60 * 1000 * 365) / 12, 'mo'],
8-
['week', 24 * 60 * 60 * 1000 * 7, 'wk'],
9-
['day', 24 * 60 * 60 * 1000, 'd'],
10-
['hour', 60 * 60 * 1000, 'h'],
11-
['minute', 60 * 1000, 'm'],
12-
['second', 1000, 's'],
5+
const relativeUnitThresholds: [Intl.RelativeTimeFormatUnit, number, number, string][] = [
6+
['year', 24 * 60 * 60 * 1000 * (365 * 2 - 1), 24 * 60 * 60 * 1000 * 365, 'yr'],
7+
['month', (24 * 60 * 60 * 1000 * 365) / 12, (24 * 60 * 60 * 1000 * 365) / 12, 'mo'],
8+
['week', 24 * 60 * 60 * 1000 * 7, 24 * 60 * 60 * 1000 * 7, 'wk'],
9+
['day', 24 * 60 * 60 * 1000, 24 * 60 * 60 * 1000, 'd'],
10+
['hour', 60 * 60 * 1000, 60 * 60 * 1000, 'h'],
11+
['minute', 60 * 1000, 60 * 1000, 'm'],
12+
['second', 1000, 1000, 's'],
1313
];
1414

1515
type DateStyle = 'full' | 'long' | 'medium' | 'short';
@@ -75,7 +75,7 @@ export function createFromDateDelta(
7575
export function fromNow(date: Date, short?: boolean): string {
7676
const elapsed = date.getTime() - new Date().getTime();
7777

78-
for (const [unit, threshold, shortUnit] of relativeUnitThresholds) {
78+
for (const [unit, threshold, divisor, shortUnit] of relativeUnitThresholds) {
7979
const elapsedABS = Math.abs(elapsed);
8080
if (elapsedABS >= threshold || threshold === 1000 /* second */) {
8181
if (short) {
@@ -95,7 +95,7 @@ export function fromNow(date: Date, short?: boolean): string {
9595
}
9696

9797
if (locale === 'en' || locale?.startsWith('en-')) {
98-
const value = Math.round(elapsedABS / threshold);
98+
const value = Math.round(elapsedABS / divisor);
9999
return `${value}${shortUnit}`;
100100
}
101101

@@ -107,7 +107,7 @@ export function fromNow(date: Date, short?: boolean): string {
107107
});
108108
}
109109

110-
return defaultShortRelativeTimeFormat.format(Math.round(elapsed / threshold), unit);
110+
return defaultShortRelativeTimeFormat.format(Math.round(elapsed / divisor), unit);
111111
}
112112

113113
if (defaultRelativeTimeFormat == null) {
@@ -117,7 +117,7 @@ export function fromNow(date: Date, short?: boolean): string {
117117
style: 'long',
118118
});
119119
}
120-
return defaultRelativeTimeFormat.format(Math.round(elapsed / threshold), unit);
120+
return defaultRelativeTimeFormat.format(Math.round(elapsed / divisor), unit);
121121
}
122122
}
123123

0 commit comments

Comments
 (0)