diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-05-02 18:36:09 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-05-02 18:36:09 +0200 |
| commit | ca921d7536cfe950b5a7d442977bbf900b48faf4 (patch) | |
| tree | 2e8bb3f4b81414ee881c3d92d9bdfed411c569db /src/utils/helpers/dates.ts | |
| parent | 9308a6dce03bd0c616e0ba6fec227473aaa44b33 (diff) | |
chore: fetch posts for rss feed
Diffstat (limited to 'src/utils/helpers/dates.ts')
| -rw-r--r-- | src/utils/helpers/dates.ts | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/utils/helpers/dates.ts b/src/utils/helpers/dates.ts new file mode 100644 index 0000000..fa167a7 --- /dev/null +++ b/src/utils/helpers/dates.ts @@ -0,0 +1,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), + }; +}; |
