Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Greenscreener
Last active March 24, 2023 13:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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

Nice one!

It seems the last seen time doesn't get changed.

I tried to change the filter to use the title and match on:

/^last seen today at \d\d?:\d\d [ap]m$/

But that doesn't seem to work unfortunately. Do you have any ideas on that?

@Greenscreener
Copy link
Author

Yeah, I knew about that, but I felt like it wasn't that big of a deal tbh. I implemented a solution but it's not as pretty imo. It works tho.

@MathijsNL
Copy link

MathijsNL commented Apr 1, 2022

Nevermind, got it working now by adding this to the bottom:

function lastseenconv12to24(str) {
	const timepart = str.substring(19)
	const convtime = conv12to24(timepart)
	return (" last seen today at " + convtime)
}

setInterval(() => {
  [
    ...document.body.getElementsByTagName("span"),
    ...document.body.getElementsByTagName("div")
  ].filter(e => e.innerText.match(/^last seen today at \d\d?:\d\d [ap]m$/))
    .forEach(e => e.innerText = lastseenconv12to24(e.innerText))
}, 500)

And thanks a lot for this script.

@Greenscreener
Copy link
Author

Nice solution! 👍

@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