From da66d1974c37d34226ca5cb2c2e5b81be01d6460 Mon Sep 17 00:00:00 2001 From: azerty29242 <94782485+azerty29242@users.noreply.github.com> Date: Thu, 16 Jan 2025 20:08:43 +0100 Subject: [PATCH 1/4] fix: improve relative date formatting --- src/utils/format/DateHelper.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/utils/format/DateHelper.ts b/src/utils/format/DateHelper.ts index 8886b750f..e5b6815de 100644 --- a/src/utils/format/DateHelper.ts +++ b/src/utils/format/DateHelper.ts @@ -13,5 +13,11 @@ export const timestampToString = (timestamp: number) => { ? "Demain" : difference === 2 ? "Après-demain" - : `Dans ${difference} jours`; + : difference === -1 + ? "Hier" + : difference === -2 + ? "Avant-hier" + : difference < 0 + ? `Il y a ${0 - difference} jours` + : `Dans ${difference} jours`; }; From 2b3c087e26deba37b1de7a2add066903278c5507 Mon Sep 17 00:00:00 2001 From: azerty29242 <94782485+azerty29242@users.noreply.github.com> Date: Fri, 17 Jan 2025 06:10:38 +0000 Subject: [PATCH 2/4] fix: use switch case statement in date helper --- src/utils/format/DateHelper.ts | 41 +++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/utils/format/DateHelper.ts b/src/utils/format/DateHelper.ts index e5b6815de..19754c44a 100644 --- a/src/utils/format/DateHelper.ts +++ b/src/utils/format/DateHelper.ts @@ -7,17 +7,32 @@ export const timestampToString = (timestamp: number) => { const difference = Math.ceil((date.getTime() - today.getTime()) / (1000 * 60 * 60 * 24)); - return difference === 0 - ? "Aujourd'hui" - : difference === 1 - ? "Demain" - : difference === 2 - ? "Après-demain" - : difference === -1 - ? "Hier" - : difference === -2 - ? "Avant-hier" - : difference < 0 - ? `Il y a ${0 - difference} jours` - : `Dans ${difference} jours`; + let formattedDate: string; + + switch (difference) { + case -2: + formattedDate = "Avant-hier"; + break; + case -1: + formattedDate = "Hier"; + break; + case 0: + formattedDate = "Aujourd'hui"; + break; + case 1: + formattedDate = "Demain"; + break; + case 2: + formattedDate = "Après-demain"; + break; + default: + if (difference < 0) { + formattedDate = `Il y a ${0 - difference} jours`; + } else { + formattedDate = `Dans ${difference} jours`; + } + break; + } + + return formattedDate; }; From e8de679d727964aae0e92fa75af67c52808898e7 Mon Sep 17 00:00:00 2001 From: azerty29242 <94782485+azerty29242@users.noreply.github.com> Date: Sat, 18 Jan 2025 10:44:46 +0000 Subject: [PATCH 3/4] fix: add months and years in relative dates --- src/utils/format/DateHelper.ts | 70 +++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/src/utils/format/DateHelper.ts b/src/utils/format/DateHelper.ts index 19754c44a..453ad79c3 100644 --- a/src/utils/format/DateHelper.ts +++ b/src/utils/format/DateHelper.ts @@ -5,33 +5,57 @@ export const timestampToString = (timestamp: number) => { today.setHours(0, 0, 0, 0); date.setHours(0, 0, 0, 0); - const difference = Math.ceil((date.getTime() - today.getTime()) / (1000 * 60 * 60 * 24)); - let formattedDate: string; - switch (difference) { - case -2: - formattedDate = "Avant-hier"; - break; - case -1: - formattedDate = "Hier"; - break; - case 0: - formattedDate = "Aujourd'hui"; - break; - case 1: - formattedDate = "Demain"; - break; - case 2: - formattedDate = "Après-demain"; - break; - default: - if (difference < 0) { - formattedDate = `Il y a ${0 - difference} jours`; + let dateDifference = [ + date.getFullYear() - today.getFullYear(), + date.getMonth() - today.getMonth(), + date.getDate() - today.getDate(), + ]; + + let yearDifference = Math.trunc( + dateDifference[0] + dateDifference[1] / 12 + dateDifference[2] / 365, + ); + + let monthDifference = Math.trunc( + dateDifference[0] * 12 + + dateDifference[1] + dateDifference[2] / 30.4, + ); + + let dayDifference = Math.round( + (date.getTime() - today.getTime()) / (1000 * 60 * 60 * 24), + ); + + if (yearDifference < -1) { + formattedDate = `Il y a ${0 - yearDifference} ans`; + } else if (yearDifference === -1) { + formattedDate = "Il y a 1 an"; + } else if (yearDifference === 1) { + formattedDate = "Dans 1 an"; + } else if (yearDifference > 1) { + formattedDate = `Dans ${yearDifference} ans`; + } else { + if (monthDifference < 0) { + formattedDate = `Il y a ${0 - monthDifference} mois`; + } else if (monthDifference > 0) { + formattedDate = `Dans ${monthDifference} mois`; + } else { + if (dayDifference < -2) { + formattedDate = `Il y a ${0 - dayDifference} jours`; + } else if (dayDifference === -2) { + formattedDate = "Avant-hier"; + } else if (dayDifference === -1) { + formattedDate = "Hier"; + } else if (dayDifference === 0) { + formattedDate = "Aujourd'hui"; + } else if (dayDifference === 1) { + formattedDate = "Demain"; + } else if (dayDifference === 2) { + formattedDate = "Après-demain"; } else { - formattedDate = `Dans ${difference} jours`; + formattedDate = `Dans ${dayDifference} jours`; } - break; + } } return formattedDate; From 623c700c8c776ab40ed30b3f795b2c37e3e8b3bd Mon Sep 17 00:00:00 2001 From: azerty29242 <94782485+azerty29242@users.noreply.github.com> Date: Sat, 18 Jan 2025 23:40:42 +0100 Subject: [PATCH 4/4] Update DateHelper.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 𝕂𝕪𝕝𝕚𝕒𝕟 <164187100+Kgeek33@users.noreply.github.com> --- src/utils/format/DateHelper.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/utils/format/DateHelper.ts b/src/utils/format/DateHelper.ts index 453ad79c3..fb0bc91aa 100644 --- a/src/utils/format/DateHelper.ts +++ b/src/utils/format/DateHelper.ts @@ -26,14 +26,10 @@ export const timestampToString = (timestamp: number) => { (date.getTime() - today.getTime()) / (1000 * 60 * 60 * 24), ); - if (yearDifference < -1) { - formattedDate = `Il y a ${0 - yearDifference} ans`; - } else if (yearDifference === -1) { - formattedDate = "Il y a 1 an"; - } else if (yearDifference === 1) { - formattedDate = "Dans 1 an"; - } else if (yearDifference > 1) { - formattedDate = `Dans ${yearDifference} ans`; + if (yearDifference <= -1) { + formattedDate = `Il y a ${0 - yearDifference} an${yearDifference < -1 ? "s" : ""}`; + } else if (yearDifference >= 1) { + formattedDate = `Dans ${yearDifference} an${yearDifference > 1 ? "s" : ""}`; } else { if (monthDifference < 0) { formattedDate = `Il y a ${0 - monthDifference} mois`;