summaryrefslogtreecommitdiffstats
path: root/src/utils/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/helpers')
-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.