summaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/format.ts
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-04-25 12:57:12 +0200
committerArmand Philippot <git@armandphilippot.com>2022-04-25 12:57:12 +0200
commit8a6f09b564d5d2f02d0a2605f6b52070a910aaa3 (patch)
treecd9e2b6ae6be75f4595b9823e67ebb6bc76df8e8 /src/utils/helpers/format.ts
parent782a5a1e794a9a8ef6b0b892cd3f386ed583c680 (diff)
chore: add a PageLayout component
Diffstat (limited to 'src/utils/helpers/format.ts')
-rw-r--r--src/utils/helpers/format.ts28
1 files changed, 25 insertions, 3 deletions
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',
@@ -284,6 +288,24 @@ export const getFormattedDate = (date: string, locale: string) => {
};
/**
+ * 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.
* @returns {ParamsSlug} An array of params with slug.