64 lines
1.3 KiB
TypeScript
64 lines
1.3 KiB
TypeScript
import { Tooltip } from "@mui/material";
|
|
import date from "date-and-time";
|
|
|
|
export function formatDate(time: number): string {
|
|
const t = new Date();
|
|
t.setTime(1000 * time);
|
|
return date.format(t, "DD/MM/YYYY HH:mm:ss");
|
|
}
|
|
|
|
export function timeDiff(a: number, b: number): string {
|
|
let diff = b - a;
|
|
|
|
if (diff === 0) return "maintenant";
|
|
if (diff === 1) return "1 seconde";
|
|
|
|
if (diff < 60) {
|
|
return `${diff} secondes`;
|
|
}
|
|
|
|
diff = Math.floor(diff / 60);
|
|
|
|
if (diff === 1) return "1 minute";
|
|
if (diff < 24) {
|
|
return `${diff} minutes`;
|
|
}
|
|
|
|
diff = Math.floor(diff / 60);
|
|
|
|
if (diff === 1) return "1 heure";
|
|
if (diff < 24) {
|
|
return `${diff} heures`;
|
|
}
|
|
|
|
const diffDays = Math.floor(diff / 24);
|
|
|
|
if (diffDays === 1) return "1 jour";
|
|
if (diffDays < 31) {
|
|
return `${diffDays} jours`;
|
|
}
|
|
|
|
diff = Math.floor(diffDays / 31);
|
|
|
|
if (diff < 12) {
|
|
return `${diff} mois`;
|
|
}
|
|
|
|
const diffYears = Math.floor(diffDays / 365);
|
|
|
|
if (diffYears === 1) return "1 an";
|
|
return `${diffYears} ans`;
|
|
}
|
|
|
|
export function timeDiffFromNow(time: number): string {
|
|
return timeDiff(time, Math.floor(new Date().getTime() / 1000));
|
|
}
|
|
|
|
export function TimeWidget(p: { time: number }): React.ReactElement {
|
|
return (
|
|
<Tooltip title={formatDate(p.time)}>
|
|
<span>{timeDiffFromNow(p.time)}</span>
|
|
</Tooltip>
|
|
);
|
|
}
|