Fix handling of date string that are ISO formatted

This commit is contained in:
Borislav Pantaleev
2025-02-27 00:03:01 +02:00
parent 341c9950f7
commit 201da84967

View File

@@ -28,18 +28,18 @@ export const dateFormatter = (v: string | number | Date | undefined | null): str
};
// assuming date is in format "2025-02-26 20:52:00" where no timezone is specified
export const getTimeSince = (date: string) => {
export const getTimeSince = (dateToCompare: string) => {
const nowUTC = new Date().getTime();
const past = new Date(date.replace(" ", "T") + "Z");
if (!dateToCompare.includes("Z")) {
dateToCompare = dateToCompare + "Z";
}
const past = new Date(dateToCompare);
const pastUTC = past.getTime();
const diffInMs = nowUTC - pastUTC;
const diffInMinutes = Math.floor(diffInMs / (1000 * 60));
// Remove or comment out the console.log for production
console.log("FOR now, date", new Date(nowUTC).toISOString(), new Date(pastUTC).toISOString(), "diffInMinutes", diffInMinutes);
if (diffInMinutes < 1) return "a couple of seconds";
if (diffInMinutes === 1) return "1 minute";
if (diffInMinutes < 60) return `${diffInMinutes} minutes`;