feat: Add date to messages if they weren't sent today

This commit is contained in:
JustTemmie 2025-07-03 18:09:25 +02:00
parent 49ad1d195d
commit 5a09b39fcf
Signed by: justtemmie
SSH key fingerprint: SHA256:nBO+OwpTkd8LYhe38PIqdxmDvkIg9Vw2EbrRZM97dkU
Notes: JustTemmie 2025-07-03 16:16:40 +00:00
actually, this should almost certainly be a global currentDate variable or something, it's WAY less memory usage, and it would make messages upload to "yesterday" in real time

View file

@ -10,6 +10,8 @@
{{ username }} {{ username }}
</span> </span>
<span class="message-date" :title="date.toString()"> <span class="message-date" :title="date.toString()">
<span v-if="getDayDifference(date, currentDate) === 1">Yesterday at</span>
<span v-else-if="getDayDifference(date, currentDate) > 1 ">{{ date.toLocaleDateString(undefined) }},</span>
{{ date.toLocaleTimeString(undefined, { timeStyle: "short" }) }} {{ date.toLocaleTimeString(undefined, { timeStyle: "short" }) }}
</span> </span>
</div> </div>
@ -49,6 +51,7 @@ const messageElement = ref<HTMLDivElement>();
const dateHidden = ref<boolean>(true); const dateHidden = ref<boolean>(true);
const date = new Date(props.timestamp); const date = new Date(props.timestamp);
const currentDate: Date = new Date()
console.log("message:", props.text); console.log("message:", props.text);
console.log("author:", props.username); console.log("author:", props.username);
@ -74,6 +77,20 @@ onMounted(async () => {
// showHover.value = !showHover.value; // showHover.value = !showHover.value;
//} //}
function getDayDifference(date_1: Date, date_2: Date) {
// Normalize both dates to midnight
const midnight1 = new Date(date_1.getFullYear(), date_1.getMonth(), date_1.getDate());
const midnight2 = new Date(date_2.getFullYear(), date_2.getMonth(), date_2.getDate());
// Calculate the difference in time
const timeDifference = midnight2.getTime() - midnight1.getTime();
// Convert time difference from milliseconds to days
const dayDifference = timeDifference / (1000 * 60 * 60 * 24);
return Math.round(dayDifference); // Round to the nearest whole number
}
</script> </script>
<style scoped> <style scoped>