From 8a6f09b564d5d2f02d0a2605f6b52070a910aaa3 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Mon, 25 Apr 2022 12:57:12 +0200 Subject: chore: add a PageLayout component --- src/utils/helpers/format.ts | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'src/utils/helpers') diff --git a/src/utils/helpers/format.ts b/src/utils/helpers/format.ts index dd35868..47a7b57 100644 --- a/src/utils/helpers/format.ts +++ b/src/utils/helpers/format.ts @@ -14,6 +14,7 @@ import { TopicPreview, Thematic, } from '@ts/types/taxonomies'; +import { settings } from '@utils/config'; /** * Format a post preview from RawArticlePreview to ArticlePreview type. @@ -269,11 +270,14 @@ export const getFormattedPost = (rawPost: RawArticle): Article => { /** * Converts a date to a string by using the specified locale. - * @param {string} date The date. - * @param {string} locale A locale. + * @param {string} date - The date. + * @param {string} [locale] - A locale. * @returns {string} The formatted date to locale date string. */ -export const getFormattedDate = (date: string, locale: string) => { +export const getFormattedDate = ( + date: string, + locale: string = settings.locales.defaultLocale +): string => { const dateOptions: Intl.DateTimeFormatOptions = { day: 'numeric', month: 'long', @@ -283,6 +287,24 @@ export const getFormattedDate = (date: string, locale: string) => { return new Date(date).toLocaleDateString(locale, dateOptions); }; +/** + * Converts a date to a time string by using the specified locale. + * @param {string} date - The date. + * @param {string} [locale] - A locale. + * @returns {string} The formatted time to locale date string. + */ +export const getFormattedTime = ( + date: string, + locale: string = settings.locales.defaultLocale +): string => { + const time = new Date(date).toLocaleTimeString(locale, { + hour: 'numeric', + minute: 'numeric', + }); + + return locale === 'fr' ? time.replace(':', 'h') : time; +}; + /** * Convert an array of slugs to an array of params with slug. * @param {Slug} array - An array of object with slug. -- cgit v1.2.3