aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/dates.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/helpers/dates.ts')
-rw-r--r--src/utils/helpers/dates.ts40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/utils/helpers/dates.ts b/src/utils/helpers/dates.ts
new file mode 100644
index 0000000..cb56ad2
--- /dev/null
+++ b/src/utils/helpers/dates.ts
@@ -0,0 +1,40 @@
+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;
+};