@@ -32,6 +32,7 @@ interface TimeAgoOptions {
32
32
33
33
/**
34
34
* Calculate how long ago a given date was and return a human-readable string.
35
+ * TODO: Consider using a specialized library.
35
36
*
36
37
* @param dateInput - The date to calculate the time difference from. It can be a Date object or an ISO 8601 string.
37
38
* @param options - Customization options for the output.
@@ -62,12 +63,12 @@ export function timeAgo(
62
63
63
64
// Define the intervals in seconds for various time units
64
65
const intervals = [
65
- { label : "year " , seconds : 31536000 } ,
66
- { label : "month " , seconds : 2592000 } ,
67
- { label : "week" , seconds : 604800 } ,
68
- { label : "day " , seconds : 86400 } ,
69
- { label : "hour " , seconds : 3600 } ,
70
- { label : "minute " , seconds : 60 } ,
66
+ { label : "jaar" , plural : "jaren" , shortFormat : "j ", seconds : 31536000 } ,
67
+ { label : "maand" , plural : "maanden" , shortFormat : "ma ", seconds : 2592000 } ,
68
+ { label : "week" , plural : "weken" , shortFormat : "w" , seconds : 604800 } ,
69
+ { label : "dag" , plural : "dagen" , shortFormat : "d ", seconds : 86400 } ,
70
+ { label : "uur" , plural : "uur" , shortFormat : "u ", seconds : 3600 } ,
71
+ { label : "minuut" , plural : "minuten" , shortFormat : "m ", seconds : 60 } ,
71
72
] ;
72
73
73
74
let result = "" ;
@@ -78,9 +79,12 @@ export function timeAgo(
78
79
if ( intervalCount >= 1 ) {
79
80
// Format the label based on short or long format
80
81
const label = shortFormat
81
- ? interval . label . charAt ( 0 ) + ( interval . label === "month" ? "o" : "" )
82
- : interval . label + ( intervalCount !== 1 ? "s" : "" ) ;
83
- result += `${ intervalCount } ${ label } ${ shortFormat ? "" : " ago" } ` ;
82
+ ? interval . shortFormat
83
+ : intervalCount === 1
84
+ ? interval . label
85
+ : interval . plural ;
86
+
87
+ result += `${ intervalCount } ${ label } ${ shortFormat ? "" : " geleden" } ` ;
84
88
// Update seconds to the remainder for the next interval
85
89
seconds %= interval . seconds ;
86
90
// If in short format, add a space for the next unit
0 commit comments