Skip to content

Instantly share code, notes, and snippets.

@YellowAfterlife
Created October 4, 2019 13:42
Show Gist options
  • Save YellowAfterlife/2c4498ace5a3e5b169d776b844ad4e6c to your computer and use it in GitHub Desktop.
Save YellowAfterlife/2c4498ace5a3e5b169d776b844ad4e6c to your computer and use it in GitHub Desktop.
(function() {
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
// default en-GB will give you DD/MM/YYYY hh:mm::ss
let dateLocales = "en-GB";
let dateOptions = undefined;
// (making it easier to restart by pasting the edited snippet)
if (window.properTimes) {
clearInterval(window.properTimes);
for (let el of document.querySelectorAll(`time:not(.proper-time)`)) {
el.classList.remove(`proper-time`);
}
}
window.properTimes = setInterval(function() {
let today = new Date().toLocaleDateString(dateLocales, dateOptions);
for (let el of document.querySelectorAll(`time:not(.proper-time)`)) {
el.classList.add(`proper-time`);
let dt_str = el.getAttribute(`datetime`);
if (!dt_str) continue; // (malformed? not really a timestamp? skip it)
let dt = new Date(parseFloat(dt_str));
let date = dt.toLocaleDateString(dateLocales, dateOptions);
let time = dt.toLocaleTimeString(dateLocales, dateOptions);
// use time if the message is from today, otherwise use date & time
el.innerText = date == today ? time : date + " " + time;
}
}, 3000); // <- update interval, in milliseconds
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment