22const customDateTimeFormatParserRegex =
33 / (?< literal > \[ .* ?\] ) | (?< year > Y Y Y Y | Y Y ) | (?< month > M { 1 , 4 } ) | (?< day > D o | D D ? ) | (?< weekday > d { 2 , 4 } ) | (?< hour > H H ? | h h ? ) | (?< minute > m m ? ) | (?< second > s s ? ) | (?< fractionalSecond > S S S ) | (?< dayPeriod > A | a ) | (?< timeZoneName > Z Z ? ) / g;
44const dateTimeFormatRegex = / (?< dateStyle > f u l l | l o n g | m e d i u m | s h o r t ) (?: \+ (?< timeStyle > f u l l | l o n g | m e d i u m | s h o r t ) ) ? / ;
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
1515type DateStyle = 'full' | 'long' | 'medium' | 'short' ;
@@ -75,7 +75,7 @@ export function createFromDateDelta(
7575export 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