Skip to content

Instantly share code, notes, and snippets.

@xydac
Created April 15, 2024 23:47
Show Gist options
  • Save xydac/23451ae163b211de474bd82b94a21795 to your computer and use it in GitHub Desktop.
Save xydac/23451ae163b211de474bd82b94a21795 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name bogleheads-relative-time
// @namespace http://tampermonkey.net/
// @version 2024-04-15
// @description try to take over the world!
// @author You
// @match https://www.bogleheads.org/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=bogleheads.org
// @grant none
// ==/UserScript==
(function() {
'use strict';
function convertToRelativeTime() {
var elements = document.getElementsByTagName("time");
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
var datetime = new Date(element.getAttribute("datetime"));
var currentTime = new Date();
var timeDiff = currentTime - datetime;
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
var months = Math.floor(days / 30);
var years = Math.floor(months / 12);
var relativeTime;
if (years > 0) {
relativeTime = years + " year" + (years > 1 ? "s" : "") + " ago";
} else if (months > 0) {
relativeTime = months + " month" + (months > 1 ? "s" : "") + " ago";
} else if (days > 0) {
relativeTime = days + " day" + (days > 1 ? "s" : "") + " ago";
} else if (hours > 0) {
relativeTime = hours + " hour" + (hours > 1 ? "s" : "") + " ago";
} else if (minutes > 0) {
relativeTime = minutes + " minute" + (minutes > 1 ? "s" : "") + " ago";
} else {
relativeTime = "just now";
}
element.textContent = relativeTime;
}
}
convertToRelativeTime();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment