@@ -25,26 +25,27 @@ const DIVISIONS: Division[] = [
25
25
* @usage ```ts
26
26
* const currentDate = new Date()
27
27
*
28
- * formatTimeAgo ( new Date().setMonth( currentDate.getMonth() - 2 ) )
28
+ * formatRelativeTime ( new Date().setMonth( currentDate.getMonth() - 2 ) )
29
29
* // 2 months ago
30
- * formatTimeAgo ( new Date().setDate( currentDate.getDate() - 1 ) )
30
+ * formatRelativeTime ( new Date().setDate( currentDate.getDate() - 1 ) )
31
31
* // yesterday
32
- * formatTimeAgo ( new Date().setDate( currentDate.getDate() - 9 ) )
32
+ * formatRelativeTime ( new Date().setDate( currentDate.getDate() - 9 ) )
33
33
* // last week
34
34
* ```
35
35
*
36
36
* @returns The relative time formatted date string.
37
37
*/
38
- export const formatTimeAgo = (
39
- date : string | number | Date ,
40
- locales ? : string | string [ ]
38
+ const formatRelativeTime = (
39
+ date : string | number | Date = new Date ( ) ,
40
+ locales ? : string | string [ ] ,
41
+ options : Intl . RelativeTimeFormatOptions = {
42
+ numeric : 'auto' ,
43
+ } ,
41
44
) => {
42
45
43
- date = date ? new Date ( date ) : new Date ( )
46
+ date = new Date ( date )
44
47
45
- const formatter = new Intl . RelativeTimeFormat ( locales , {
46
- numeric : 'auto' ,
47
- } )
48
+ const formatter = new Intl . RelativeTimeFormat ( locales , options )
48
49
49
50
let duration = ( date . getTime ( ) - new Date ( ) . getTime ( ) ) / 1000
50
51
@@ -55,4 +56,6 @@ export const formatTimeAgo = (
55
56
}
56
57
duration /= division ! . amount
57
58
}
58
- }
59
+ }
60
+
61
+ export default formatRelativeTime
0 commit comments