aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/dates.ts
blob: cb56ad28c49af6e19c3eb211dd8561a9ff709cd9 (plain)
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
import { settings } from '@utils/config';

/**
 * Format a date based on a locale.
 *
 * @param {string} date - The date.
 * @param {string} [locale] - A locale.
 * @returns {string} The locale date string.
 */
export const getFormattedDate = (
  date: string,
  locale: string = settings.locales.defaultLocale
): string => {
  const dateOptions: Intl.DateTimeFormatOptions = {
    day: 'numeric',
    month: 'long',
    year: 'numeric',
  };

  return new Date(date).toLocaleDateString(locale, dateOptions);
};

/**
 * Format a time based on a locale.
 *
 * @param {string} time - The time.
 * @param {string} [locale] - A locale.
 * @returns {string} The locale time string.
 */
export const getFormattedTime = (
  time: string,
  locale: string = settings.locales.defaultLocale
): string => {
  const formattedTime = new Date(time).toLocaleTimeString(locale, {
    hour: 'numeric',
    minute: 'numeric',
  });

  return locale === 'fr' ? formattedTime.replace(':', 'h') : formattedTime;
};