summaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/dates.ts
blob: fa167a725546dc92e044c2b60b75aeb437451814 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { Dates } from '@ts/types/app';
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;
};

/**
 * Retrieve a Dates object.
 *
 * @param publication - The publication date.
 * @param update - The update date.
 * @returns {Dates} A Dates object.
 */
export const getDates = (publication: string, update: string): Dates => {
  return {
    publication: getFormattedDate(publication),
    update: getFormattedDate(update),
  };
};