# All files / src/lib date-format.ts

0% Statements 0/100
0% Branches 0/95
0% Functions 0/13
0% Lines 0/98

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304

     /**
     * Date formatting utilities with i18n support
     *
     * Provides consistent date and time formatting across the application
     * based on the user's locale.
     */

    import i18n from '@/lib/i18n';

    /**
     * Get the current locale from i18n
     */
    export function getCurrentLocale(): string {
      return i18n.language || 'en';
    }

    /**
     * Format a date according to the current locale
     *
     * @param date - Date to format (Date object, timestamp, or ISO string)
     * @param options - Intl.DateTimeFormat options
     * @returns Formatted date string
     */
    export function formatDate(
      date: Date | number | string,
      options?: Intl.DateTimeFormatOptions
    ): string {
      const dateObj = typeof date === 'string' || typeof date === 'number'
        ? new Date(date)
        : date;

      if (isNaN(dateObj.getTime())) {
        return 'Invalid Date';
      }

      const locale = getCurrentLocale();
      const defaultOptions: Intl.DateTimeFormatOptions = {
        year: 'numeric',
        month: 'short',
        day: 'numeric'};

      return new Intl.DateTimeFormat(locale, options || defaultOptions).format(dateObj);
    }

    /**
     * Format a date with time according to the current locale
     *
     * @param date - Date to format
     * @param options - Intl.DateTimeFormat options
     * @returns Formatted date and time string
     */
    export function formatDateTime(
      date: Date | number | string,
      options?: Intl.DateTimeFormatOptions
    ): string {
      const dateObj = typeof date === 'string' || typeof date === 'number'
        ? new Date(date)
        : date;

      if (isNaN(dateObj.getTime())) {
        return 'Invalid Date';
      }

      const locale = getCurrentLocale();
      const defaultOptions: Intl.DateTimeFormatOptions = {
        year: 'numeric',
        month: 'short',
        day: 'numeric',
        hour: '2-digit',
        minute: '2-digit'};

      return new Intl.DateTimeFormat(locale, options || defaultOptions).format(dateObj);
    }

    /**
     * Format time only according to the current locale
     *
     * @param date - Date to format
     * @param options - Intl.DateTimeFormat options
     * @returns Formatted time string
     */
    export function formatTime(
      date: Date | number | string,
      options?: Intl.DateTimeFormatOptions
    ): string {
      const dateObj = typeof date === 'string' || typeof date === 'number'
        ? new Date(date)
        : date;

      if (isNaN(dateObj.getTime())) {
        return 'Invalid Time';
      }

      const locale = getCurrentLocale();
      const defaultOptions: Intl.DateTimeFormatOptions = {
        hour: '2-digit',
        minute: '2-digit'};

      return new Intl.DateTimeFormat(locale, options || defaultOptions).format(dateObj);
    }

    /**
     * Format a date as a short date (e.g., "12/31/2023" or "31/12/2023")
     *
     * @param date - Date to format
     * @returns Short formatted date string
     */
    export function formatShortDate(date: Date | number | string): string {
      return formatDate(date, {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit'});
    }

    /**
     * Format a date as a long date (e.g., "December 31, 2023")
     *
     * @param date - Date to format
     * @returns Long formatted date string
     */
    export function formatLongDate(date: Date | number | string): string {
      return formatDate(date, {
        year: 'numeric',
        month: 'long',
        day: 'numeric'});
    }

    /**
     * Format a date as a relative time (e.g., "2 hours ago", "in 3 days")
     *
     * @param date - Date to format
     * @returns Relative time string
     */
    export function formatRelativeTime(date: Date | number | string): string {
      const dateObj = typeof date === 'string' || typeof date === 'number'
        ? new Date(date)
        : date;

      if (isNaN(dateObj.getTime())) {
        return 'Invalid Date';
      }

      const locale = getCurrentLocale();
      const now = new Date();
      const diffMs = dateObj.getTime() - now.getTime();
      const diffSec = Math.round(diffMs / 1000);
      const diffMin = Math.round(diffSec / 60);
      const diffHour = Math.round(diffMin / 60);
      const diffDay = Math.round(diffHour / 24);
      const diffWeek = Math.round(diffDay / 7);
      const diffMonth = Math.round(diffDay / 30);
      const diffYear = Math.round(diffDay / 365);

      const rtf = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' });

      if (Math.abs(diffSec) < 60) {
        return rtf.format(diffSec, 'second');
      } else if (Math.abs(diffMin) < 60) {
        return rtf.format(diffMin, 'minute');
      } else if (Math.abs(diffHour) < 24) {
        return rtf.format(diffHour, 'hour');
      } else if (Math.abs(diffDay) < 7) {
        return rtf.format(diffDay, 'day');
      } else if (Math.abs(diffWeek) < 4) {
        return rtf.format(diffWeek, 'week');
      } else if (Math.abs(diffMonth) < 12) {
        return rtf.format(diffMonth, 'month');
      } else {
        return rtf.format(diffYear, 'year');
      }
    }

    /**
     * Format a date range according to the current locale
     *
     * @param startDate - Start date
     * @param endDate - End date
     * @param options - Intl.DateTimeFormat options
     * @returns Formatted date range string
     */
    export function formatDateRange(
      startDate: Date | number | string,
      endDate: Date | number | string,
      options?: Intl.DateTimeFormatOptions
    ): string {
      const startObj = typeof startDate === 'string' || typeof startDate === 'number'
        ? new Date(startDate)
        : startDate;
      const endObj = typeof endDate === 'string' || typeof endDate === 'number'
        ? new Date(endDate)
        : endDate;

      if (isNaN(startObj.getTime()) || isNaN(endObj.getTime())) {
        return 'Invalid Date Range';
      }

      const locale = getCurrentLocale();
      const defaultOptions: Intl.DateTimeFormatOptions = {
        year: 'numeric',
        month: 'short',
        day: 'numeric'};

      const formatter = new Intl.DateTimeFormat(locale, options || defaultOptions);
      return formatter.formatRange(startObj, endObj);
    }

    /**
     * Format duration in seconds to human-readable format
     *
     * @param seconds - Duration in seconds
     * @returns Formatted duration string (e.g., "1h 23m", "45m", "2h")
     */
    export function formatDuration(seconds: number): string {
      if (seconds < 0 || isNaN(seconds)) {
        return '0m';
      }

      const hours = Math.floor(seconds / 3600);
      const minutes = Math.floor((seconds % 3600) / 60);

      if (hours > 0 && minutes > 0) {
        return `${hours}h ${minutes}m`;
      } else if (hours > 0) {
        return `${hours}h`;
      } else {
        return `${minutes}m`;
      }
    }

    /**
     * Format file size to human-readable format
     *
     * @param bytes - Size in bytes
     * @returns Formatted size string (e.g., "1.5 MB", "500 KB")
     */
    export function formatFileSize(bytes: number): string {
      if (bytes === 0) return '0 B';
      if (bytes < 0 || isNaN(bytes)) return 'Invalid Size';

      const k = 1024;
      const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
      const i = Math.floor(Math.log(bytes) / Math.log(k));

      return `${(bytes / Math.pow(k, i)).toFixed(2)} ${sizes[i]}`;
    }

    /**
     * Check if a date is today
     *
     * @param date - Date to check
     * @returns True if the date is today
     */
    export function isToday(date: Date | number | string): boolean {
      const dateObj = typeof date === 'string' || typeof date === 'number'
        ? new Date(date)
        : date;

      if (isNaN(dateObj.getTime())) {
        return false;
      }

      const today = new Date();
      return dateObj.getDate() === today.getDate() &&
        dateObj.getMonth() === today.getMonth() &&
        dateObj.getFullYear() === today.getFullYear();
    }

    /**
     * Check if a date is in the past
     *
     * @param date - Date to check
     * @returns True if the date is in the past
     */
    export function isPast(date: Date | number | string): boolean {
      const dateObj = typeof date === 'string' || typeof date === 'number'
        ? new Date(date)
        : date;

      if (isNaN(dateObj.getTime())) {
        return false;
      }

      return dateObj.getTime() < Date.now();
    }

    /**
     * Check if a date is in the future
     *
     * @param date - Date to check
     * @returns True if the date is in the future
     */
    export function isFuture(date: Date | number | string): boolean {
      const dateObj = typeof date === 'string' || typeof date === 'number'
        ? new Date(date)
        : date;

      if (isNaN(dateObj.getTime())) {
        return false;
      }

      return dateObj.getTime() > Date.now();
    }