-
-
Save gabeweb/ba14d475bfe0a9b33fbe1006b1fe4766 to your computer and use it in GitHub Desktop.
Convert all elements with 12-hour time to 24-hour automatically (for almost any website)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name 12 hour am pm to 24 hour format | |
// @namespace https://* | |
// @version 0.1 | |
// @description Convert all elements with 12-hour time to 24-hour automatically for all websites. Tested: Discord, Cloud Hetzner Console | |
// @author gabeweb | |
// @homepage https://gist.github.com/gabeweb/ba14d475bfe0a9b33fbe1006b1fe4766 | |
// @icon https://raw.githubusercontent.com/dw5/24H-BrowserTimeEverywhere/main/icons/128.png | |
// @grant none | |
// @match https://*/* | |
// @run-at document-start | |
// @license MIT | |
// @downloadURL https://gist.github.com/gabeweb/ba14d475bfe0a9b33fbe1006b1fe4766 | |
// ==/UserScript== | |
function convertTimeTo24HourFormat() { | |
var timeElements = document.querySelectorAll('body :not(script):not(noscript):not(style):not(textarea):not(input)'); | |
timeElements.forEach(function (element) { | |
var nodes = element.childNodes; | |
for (var i = 0; i < nodes.length; i++) { | |
var node = nodes[i]; | |
if (node.nodeType === Node.TEXT_NODE) { | |
var text = node.nodeValue; | |
var regex = /\b(\d{1,2}):(\d{2})(?::(\d{2}))?\s*([ap])\.?\s*m\.?\b/gi; | |
var convertedText = text.replace(regex, function (match, hours, minutes, seconds, meridiem) { | |
var hour = parseInt(hours, 10); | |
if (hour === 12) { | |
hour = meridiem.toLowerCase() === 'a' ? 0 : 12; | |
} else { | |
hour = meridiem.toLowerCase() === 'a' ? hour : hour + 12; | |
} | |
return ('0' + hour).slice(-2) + ':' + minutes + (seconds ? (':' + seconds) : ''); | |
}); | |
if (convertedText !== text) { | |
var newTextNode = document.createTextNode(convertedText); | |
element.replaceChild(newTextNode, node); | |
} | |
} | |
} | |
}); | |
} | |
setInterval(convertTimeTo24HourFormat, 500); // half of 1 second delay (0.5 s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a fork from 24H-BrowserTimeEverywhere, because some non-correct websites leave a white space between a. (or p.) and m.. So...
For applying this script, you can use: