Skip to content

Commit 92415d2

Browse files
committed
2 parents d624fc7 + a73ee13 commit 92415d2

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

src/utils/format/DateHelper.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
export const timestampToString = (timestamp: number) => {
2+
3+
if (!timestamp || Number.isNaN(timestamp)) {
4+
return "Date invalide";
5+
}
6+
27
const date = new Date(timestamp);
38
const today = new Date();
9+
if (Number.isNaN(date.getTime())) {
10+
return "Date invalide";
11+
}
412

513
today.setHours(0, 0, 0, 0);
614
date.setHours(0, 0, 0, 0);
@@ -13,32 +21,37 @@ export const timestampToString = (timestamp: number) => {
1321
date.getDate() - today.getDate(),
1422
];
1523

16-
let yearDifference = Math.trunc(
17-
dateDifference[0] + dateDifference[1] / 12 + dateDifference[2] / 365
18-
);
19-
20-
let monthDifference = Math.trunc(
21-
dateDifference[0] * 12 + dateDifference[1] + dateDifference[2] / 30.4
22-
);
24+
let yearDifference = dateDifference[0];
25+
if (dateDifference[1] < 0 || (dateDifference[1] === 0 && dateDifference[2] < 0)) {
26+
yearDifference--;
27+
} else if (dateDifference[1] > 0 || (dateDifference[1] === 0 && dateDifference[2] > 0)) {
28+
yearDifference++;
29+
}
30+
let monthDifference = dateDifference[0] * 12 + dateDifference[1];
2331

32+
if (dateDifference[2] < 0) {
33+
monthDifference--;
34+
} else if (dateDifference[2] > 0) {
35+
monthDifference++;
36+
}
2437
let dayDifference = Math.round(
2538
(date.getTime() - today.getTime()) / (1000 * 60 * 60 * 24)
2639
);
2740

28-
if (yearDifference <= -1) {
29-
formattedDate = `Il y a ${0 - yearDifference} an${
30-
yearDifference < -1 ? "s" : ""
41+
if (yearDifference < 0) {
42+
formattedDate = `Il y a ${Math.abs(yearDifference)} an${
43+
Math.abs(yearDifference) > 1 ? "s" : ""
3144
}`;
32-
} else if (yearDifference >= 1) {
45+
} else if (yearDifference > 0) {
3346
formattedDate = `Dans ${yearDifference} an${yearDifference > 1 ? "s" : ""}`;
3447
} else {
3548
if (monthDifference < 0) {
36-
formattedDate = `Il y a ${0 - monthDifference} mois`;
49+
formattedDate = `Il y a ${Math.abs(monthDifference)} mois`;
3750
} else if (monthDifference > 0) {
3851
formattedDate = `Dans ${monthDifference} mois`;
3952
} else {
4053
if (dayDifference < -2) {
41-
formattedDate = `Il y a ${0 - dayDifference} jours`;
54+
formattedDate = `Il y a ${Math.abs(dayDifference)} jours`;
4255
} else if (dayDifference === -2) {
4356
formattedDate = "Avant-hier";
4457
} else if (dayDifference === -1) {

src/views/account/Chat/Messages.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import InsetsBottomView from "@/components/Global/InsetsBottomView";
3737
import { TabLocation } from "pawnote";
3838
import {hasFeatureAccountSetup} from "@/utils/multiservice";
3939
import {MultiServiceFeature} from "@/stores/multiService/types";
40+
import { timestampToString } from "@/utils/format/DateHelper";
4041

4142
// Voir la documentation de `react-navigation`.
4243
//
@@ -242,8 +243,8 @@ const Discussions: Screen<"Discussions"> = ({ navigation, route }) => {
242243
)}
243244
<NativeText variant={"subtitle"}>{getChatCreator(chat)}</NativeText>
244245
</View>
245-
<NativeText>{chat.subject || "Aucun sujet"}</NativeText>
246-
<NativeText variant={"subtitle"}>Il y a {Math.floor((new Date().getTime() - new Date(chat.date).getTime()) / (1000 * 60 * 60 * 24))} jours</NativeText>
246+
<NativeText>{chat.subject || "Aucun sujet"}</NativeText>
247+
<NativeText variant={"subtitle"}>{timestampToString(chat.date.getTime())}</NativeText>
247248
</NativeItem>
248249
))}
249250
</NativeList>

0 commit comments

Comments
 (0)