Skip to content

Instantly share code, notes, and snippets.

@mattogodoy
Created June 7, 2021 14:41
Show Gist options
  • Select an option

  • Save mattogodoy/515b6060ff75f8b104cd56cfad52c027 to your computer and use it in GitHub Desktop.

Select an option

Save mattogodoy/515b6060ff75f8b104cd56cfad52c027 to your computer and use it in GitHub Desktop.
Violent Monkey script to improve readability of dates in StackOverflow
// ==UserScript==
// @name Improved dates - stackoverflow.com
// @namespace Violentmonkey Scripts
// @match https://stackoverflow.com/questions/*
// @grant none
// @version 1.0
// @author Matto
// @description 11/11/2020, 15:40:59
// ==/UserScript==
// Wait for the page to fully load
window.onload = function(){
addDates();
};
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
function getDate(item, isQuestion=false){
if(isQuestion){
var dateEl = document.getElementsByTagName('time')[0];
} else {
var dateEl = item.getElementsByTagName('time')[0];
}
var date = dateEl.getAttribute('datetime');
return formatDate(date);
}
function appendDate(item, date){
var html = '';
html += '<div style="color: #ffff4b; font-size: 16px; font-weight: bold; padding-left: 52px;">';
html += ' <p>' + date + '</div>';
html += '</div>';
var el = document.createElement('div');
el.className = 'better-date';
el.innerHTML = html;
item.prepend(el);
}
function addDates(){
var question = document.getElementsByClassName('question')[0];
var answers = document.getElementsByClassName('answer');
// Add dates to question
appendDate(question, getDate(question, true));
// Add dates to answers
for (let item of answers) {
appendDate(item, getDate(item));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment