Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meilleur affichage des dates relatives #650

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 54 additions & 9 deletions src/utils/format/DateHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,58 @@ 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));

return difference === 0
? "Aujourd'hui"
: difference === 1
? "Demain"
: difference === 2
? "Après-demain"
: `Dans ${difference} jours`;
let formattedDate: string;

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 {
azerty29242 marked this conversation as resolved.
Show resolved Hide resolved
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 ${dayDifference} jours`;
}
}
}

return formattedDate;
};