@@ -21,45 +21,46 @@ function parse(maybeDate: Date | string) {
2121 return null ;
2222}
2323
24- export default {
25- /**
26- * Returns a human-friendly relative time string from now.
27- * For very recent dates, returns specific labels (e.g., 'JustNow').
28- * @param date - Date or string to compare
29- * @returns Relative time string or empty string if invalid
30- */
31- distanceInWordsToNow ( date : Date | string ) {
32- const parsed = parse ( date ) ;
24+ /**
25+ * Returns a human-friendly relative time string from now.
26+ * For very recent dates, returns specific labels (e.g., 'JustNow').
27+ * @param date - Date or string to compare
28+ * @returns Relative time string or empty string if invalid
29+ */
30+ export function distanceInWordsToNow ( date : Date | string ) {
31+ const parsed = parse ( date ) ;
3332
34- if ( ! parsed ) return '' ;
33+ if ( ! parsed ) return '' ;
3534
36- const diffInMs = Math . abs ( differenceInMilliseconds ( new Date ( ) , parsed ) ) ;
35+ const diffInMs = Math . abs ( differenceInMilliseconds ( new Date ( ) , parsed ) ) ;
3736
38- if ( diffInMs < 10000 ) return i18next . t ( 'formatDate.JustNow' ) ;
39- if ( diffInMs < 20000 ) return i18next . t ( 'formatDate.15Seconds' ) ;
40- if ( diffInMs < 30000 ) return i18next . t ( 'formatDate.25Seconds' ) ;
41- if ( diffInMs < 46000 ) return i18next . t ( 'formatDate.35Seconds' ) ;
37+ if ( diffInMs < 10000 ) return i18next . t ( 'formatDate.JustNow' ) ;
38+ if ( diffInMs < 20000 ) return i18next . t ( 'formatDate.15Seconds' ) ;
39+ if ( diffInMs < 30000 ) return i18next . t ( 'formatDate.25Seconds' ) ;
40+ if ( diffInMs < 46000 ) return i18next . t ( 'formatDate.35Seconds' ) ;
4241
43- const timeAgo = formatDistanceToNow ( parsed , {
44- includeSeconds : false ,
45- locale : currentDateLocale ( )
46- } ) ;
42+ const timeAgo = formatDistanceToNow ( parsed , {
43+ includeSeconds : false ,
44+ locale : currentDateLocale ( )
45+ } ) ;
4746
48- return i18next . t ( 'formatDate.Ago' , { timeAgo } ) ;
49- } ,
47+ return i18next . t ( 'formatDate.Ago' , { timeAgo } ) ;
48+ }
5049
51- /**
52- * Formats a date as a string. Includes time by default.
53- * @param date - Date or string to format
54- * @param options - Formatting options
55- * @param options.showTime - Whether to include time (default true)
56- * @returns Formatted date string or empty string if invalid
57- */
58- format ( date : Date | string , { showTime = true } = { } ) : string {
59- const parsed = parse ( date ) ;
60- if ( ! parsed ) return '' ;
50+ /**
51+ * Formats a date as a string. Includes time by default.
52+ * @param date - Date or string to format
53+ * @param options - Formatting options
54+ * @param options.showTime - Whether to include time (default true)
55+ * @returns Formatted date string or empty string if invalid
56+ */
57+ export function formatDateToString (
58+ date : Date | string ,
59+ { showTime = true } = { }
60+ ) : string {
61+ const parsed = parse ( date ) ;
62+ if ( ! parsed ) return '' ;
6163
62- const formatType = showTime ? 'PPpp' : 'PP' ;
63- return format ( parsed , formatType , { locale : currentDateLocale ( ) } ) ;
64- }
65- } ;
64+ const formatType = showTime ? 'PPpp' : 'PP' ;
65+ return format ( parsed , formatType , { locale : currentDateLocale ( ) } ) ;
66+ }
0 commit comments