11'use strict' ;
2- import * as moment from 'moment' ;
2+ import { distanceInWordsToNow as _fromNow , format as _format } from 'date-fns' ;
3+ import * as en from 'date-fns/locale/en' ;
34
45const MillisecondsPerMinute = 60000 ; // 60 * 1000
56const MillisecondsPerDay = 86400000 ; // 24 * 60 * 60 * 1000
67
8+ // Taken from https://github.com/date-fns/date-fns/blob/601bc8e5708cbaebee5389bdaf51c2b4b33b73c4/src/locale/en/build_distance_in_words_locale/index.js
9+ function buildDistanceInWordsLocale ( ) {
10+ const distanceInWordsLocale : { [ key : string ] : string | { one : string , other : string } } = {
11+ lessThanXSeconds : {
12+ one : 'less than a second' ,
13+ other : 'less than {{count}} seconds'
14+ } ,
15+
16+ xSeconds : {
17+ one : '1 second' ,
18+ other : '{{count}} seconds'
19+ } ,
20+
21+ halfAMinute : 'half a minute' ,
22+
23+ lessThanXMinutes : {
24+ one : 'a few seconds' ,
25+ other : 'less than {{count}} minutes'
26+ } ,
27+
28+ xMinutes : {
29+ one : 'a minute' ,
30+ other : '{{count}} minutes'
31+ } ,
32+
33+ aboutXHours : {
34+ one : 'an hour' ,
35+ other : '{{count}} hours'
36+ } ,
37+
38+ xHours : {
39+ one : 'an hour' ,
40+ other : '{{count}} hours'
41+ } ,
42+
43+ xDays : {
44+ one : 'a day' ,
45+ other : '{{count}} days'
46+ } ,
47+
48+ aboutXMonths : {
49+ one : 'a month' ,
50+ other : '{{count}} months'
51+ } ,
52+
53+ xMonths : {
54+ one : 'a month' ,
55+ other : '{{count}} months'
56+ } ,
57+
58+ aboutXYears : {
59+ one : 'a year' ,
60+ other : '{{count}} years'
61+ } ,
62+
63+ xYears : {
64+ one : 'a year' ,
65+ other : '{{count}} years'
66+ } ,
67+
68+ overXYears : {
69+ one : 'a year' ,
70+ other : '{{count}} years'
71+ } ,
72+
73+ almostXYears : {
74+ one : 'a year' ,
75+ other : '{{count}} years'
76+ }
77+ } ;
78+
79+ function localize ( token : string , count : number , options : any ) {
80+ options = options || { } ;
81+
82+ const result = distanceInWordsLocale [ token ] ;
83+
84+ let value : string ;
85+ if ( typeof result === 'string' ) {
86+ value = result ;
87+ }
88+ else {
89+ if ( count === 12 && token === 'xMonths' ) {
90+ token = 'aboutXYears' ;
91+ count = 1 ;
92+ }
93+
94+ if ( count === 1 ) {
95+ value = result . one ;
96+ }
97+ else {
98+ value = result . other . replace ( '{{count}}' , count . toString ( ) ) ;
99+ }
100+ }
101+
102+ if ( ! options . addSuffix ) return value ;
103+
104+ if ( options . comparison > 0 ) return 'in ' + value ;
105+
106+ return value + ' ago' ;
107+ }
108+
109+ return {
110+ localize : localize
111+ } ;
112+ }
113+
114+ // Monkey patch the locale to customize the wording
115+ ( en as any ) . distanceInWords = buildDistanceInWordsLocale ( ) ;
116+
117+ const formatterOptions = { addSuffix : true , locale : en } ;
118+
7119export namespace Dates {
8120
9121 export interface IDateFormatter {
@@ -28,6 +140,9 @@ export namespace Dates {
28140 }
29141
30142 export function toFormatter ( date : Date ) : IDateFormatter {
31- return moment ( date ) ;
143+ return {
144+ fromNow : ( ) => _fromNow ( date , formatterOptions ) ,
145+ format : ( format : string ) => _format ( date , format )
146+ } ;
32147 }
33148}
0 commit comments