From 887ee8aea47247e720fd6afd86468d69a4f49596 Mon Sep 17 00:00:00 2001 From: wbuezas Date: Wed, 14 Aug 2024 11:28:15 -0300 Subject: [PATCH] Create date format util with translations --- src/components/ui/TicketDetails.vue | 6 ++-- src/i18n/ca-ES/index.js | 2 +- src/i18n/en-US/index.js | 2 +- src/i18n/es-ES/index.js | 2 +- src/i18n/fr-FR/index.js | 2 +- src/i18n/pt-PT/index.js | 2 +- src/lib/filters.js | 47 +++++++++++++++++++++++------ src/pages/Ecomerce/Catalog.vue | 5 ++- src/pages/Ecomerce/InvoicesView.vue | 6 ++-- 9 files changed, 50 insertions(+), 24 deletions(-) diff --git a/src/components/ui/TicketDetails.vue b/src/components/ui/TicketDetails.vue index eb05eb34..37af51ab 100644 --- a/src/components/ui/TicketDetails.vue +++ b/src/components/ui/TicketDetails.vue @@ -29,7 +29,7 @@ const lineSubtotal = line =>
#{{ ticket.id }}
- +
{{ t('shippingInformation') }}
@@ -39,14 +39,14 @@ const lineSubtotal = line =>
{{ t('delivery') }} - {{ formatDateTitle(ticket.shipped) }} + {{ formatDateTitle(ticket.landed) }}
{{ t(ticket.method != 'PICKUP' ? 'agency' : 'warehouse') }} {{ ticket.agency }}
- +
{{ t('deliveryAddress') }}
diff --git a/src/i18n/ca-ES/index.js b/src/i18n/ca-ES/index.js index cdf4b9ef..0519209d 100644 --- a/src/i18n/ca-ES/index.js +++ b/src/i18n/ca-ES/index.js @@ -24,7 +24,7 @@ export default { 'Novembre', 'Desembre' ], - shortMonths: [ + monthsShort: [ 'Gen', 'Feb', 'Mar', diff --git a/src/i18n/en-US/index.js b/src/i18n/en-US/index.js index 8d4615fe..e4648200 100644 --- a/src/i18n/en-US/index.js +++ b/src/i18n/en-US/index.js @@ -37,7 +37,7 @@ export default { 'November', 'December' ], - shortMonths: [ + monthsShort: [ 'Jan', 'Feb', 'Mar', diff --git a/src/i18n/es-ES/index.js b/src/i18n/es-ES/index.js index 82b4d6a8..ea4d53c9 100644 --- a/src/i18n/es-ES/index.js +++ b/src/i18n/es-ES/index.js @@ -46,7 +46,7 @@ export default { 'Noviembre', 'Diciembre' ], - shortMonths: [ + monthsShort: [ 'Ene', 'Feb', 'Mar', diff --git a/src/i18n/fr-FR/index.js b/src/i18n/fr-FR/index.js index d2ff0528..405cd12a 100644 --- a/src/i18n/fr-FR/index.js +++ b/src/i18n/fr-FR/index.js @@ -24,7 +24,7 @@ export default { 'Novembre', 'Décembre' ], - shortMonths: [ + monthsShort: [ 'Jan', 'Fév', 'Mar', diff --git a/src/i18n/pt-PT/index.js b/src/i18n/pt-PT/index.js index f5e8eb51..c2b63f90 100644 --- a/src/i18n/pt-PT/index.js +++ b/src/i18n/pt-PT/index.js @@ -24,7 +24,7 @@ export default { 'Novembro', 'Dezembro' ], - shortMonths: [ + monthsShort: [ 'Jan', 'Fev', 'Mar', diff --git a/src/lib/filters.js b/src/lib/filters.js index 88d3f33b..ac773e10 100644 --- a/src/lib/filters.js +++ b/src/lib/filters.js @@ -14,16 +14,43 @@ export function date(val, format) { return qdate.formatDate(val, format, i18n.global.tm('date')); } -export const formatDateTitle = timeStamp => { - const { t, messages, locale } = i18n.global; - const formattedString = qdate.formatDate( - timeStamp, - `dddd, D [${t('of')}] MMMM [${t('of')}] YYYY`, - { - days: messages.value[locale.value].date.days, - months: messages.value[locale.value].date.months - } - ); +export const formatDate = (timeStamp, format = 'YYYY-MM-DD') => { + if (!timeStamp) return ''; + const { messages, locale } = i18n.global; + + return qdate.formatDate(timeStamp, format, { + days: messages.value[locale.value].date.days, + months: messages.value[locale.value].date.months, + daysShort: messages.value[locale.value].date.daysShort, + monthsShort: messages.value[locale.value].date.monthsShort + }); +}; + +/** + * @param {Date} timeStamp - La marca de tiempo que se va a formatear. Si no se proporciona, la función devolverá una cadena vacía. + * @param {Object} options - Un objeto que contiene las opciones de formato. + * @param {boolean} options.showTime - Indica si se debe mostrar la hora en el formato de la fecha. + * @param {boolean} options.showSeconds - Indica si se deben mostrar los segundos en el formato de la hora. Solo se aplica si showTime es true. + * @param {boolean} options.shortDay - Indica si se debe usar una versión corta del día (por ejemplo, "Mon" en lugar de "Monday"). + * @returns {string} La fecha formateada como un título. + */ +export const formatDateTitle = ( + timeStamp, + options = { showTime: false, showSeconds: false, shortDay: false } +) => { + if (!timeStamp) return ''; + const { t } = i18n.global; + + const timeFormat = options.showTime + ? options.showSeconds + ? ` [${t('at')}] hh:mm:ss` + : ` [${t('at')}] hh:mm` + : ''; + const day = options.shortDay ? 'dd' : 'dddd'; + + const formatString = `${day}, D [${t('of')}] MMMM [${t('of')}] YYYY${timeFormat}`; + + const formattedString = formatDate(timeStamp, formatString); return formattedString; }; diff --git a/src/pages/Ecomerce/Catalog.vue b/src/pages/Ecomerce/Catalog.vue index 2d712bff..6543cc05 100644 --- a/src/pages/Ecomerce/Catalog.vue +++ b/src/pages/Ecomerce/Catalog.vue @@ -343,8 +343,7 @@