Skip to content

Instantly share code, notes, and snippets.

@Greenscreener
Last active March 24, 2023 13:54
Show Gist options
  • Save Greenscreener/4422f5be36752bc720cbc7222fb73cdc to your computer and use it in GitHub Desktop.
Save Greenscreener/4422f5be36752bc720cbc7222fb73cdc to your computer and use it in GitHub Desktop.
Convert all elements with 12-hour time to 24-hour automatically (for WhatsApp Web, but could be adapted easily)
// ==UserScript==
// @name WAWeb 24-hourifier
// @version 0.10
// @description Convert all elements with 12-hour time to 24-hour automatically (for WhatsApp Web, but could be adapted easily)
// @author Greenscreener
// @homepage https://grsc.cz/
// @match https://web.whatsapp.com/*
// @grant none
// @downloadURL https://gist.githubusercontent.com/Greenscreener/4422f5be36752bc720cbc7222fb73cdc/raw/12to24.user.js
// @updateURL https://gist.githubusercontent.com/Greenscreener/4422f5be36752bc720cbc7222fb73cdc/raw/12to24.user.js
// ==/UserScript==
{
const re = /^((?:last seen \w+ at )?)(\d\d?):(\d\d) ([ap])(?:\.\u00A0)?m\.?$/
function conv12to24(str) {
const hr = parseInt(str.match(re)[2])
const mi = parseInt(str.match(re)[3])
const add = (str.match(re)[4] === "p") !== (hr === 12) // Why is 12-hour time so dumb
return str.match(re)[1]+((hr+12*add)%24).toString()+":"+mi.toString().padStart(2,"0")
}
setInterval(() => {
[
...document.body.getElementsByTagName("span"),
...document.body.getElementsByTagName("div")
].filter(e => e.innerText.match(re))
.forEach(e => e.innerText = conv12to24(e.innerText))
}, 500)
}
@MathijsNL
Copy link

At 12:xx the time is shown wrong btw. Adding an AND operator and check for hr !=12 fixes this.

const add = str.match(re)[3] === "p" && hr != 12

Also I made a standalone Chrome extension for this, I credited you:
https://github.com/MathijsNL/WhatsappTime

@rob-vandenberg
Copy link

Love this solution. Thank you very very much.

I just noticed that the Status Updates still use 12 hours am/pm notation.

@MathijsNL
Copy link

@rob-vandenberg If you check whatsapptime.js in my repository it will contain the fix for that.

@rob-vandenberg
Copy link

rob-vandenberg commented Apr 3, 2022

I think you misunderstood me. I was talking about the "Status" window in Whatsapp.
That's the window you see when you click on the left of the 3 icons at the top of the Whatsapp window, just above the contacts list.
The left icon that looks like a circle consisting of 3 parts. That one still shows AM/PM, also with the new code (I tried it).

image

@MathijsNL
Copy link

Ah that one. I never clicked that button and it is empty for me unfortunately. I will try to add a fix to that as soon as someone in my contact list does a status update.

@Greenscreener
Copy link
Author

Yeah, same problem here. To be perfectly honest, I expect whatsapp will fix this sooner than I actually receive a status from someone...
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment