GitHub Profile Time Converter

Convert GitHub profile local time from 24-hour format (e.g. “00:00”) to the 12-hour AM/PM style.

  1. // ==UserScript==
  2. // @name GitHub Profile Time Converter
  3. // @namespace https://github.com/GooglyBlox
  4. // @version 1.1
  5. // @description Convert GitHub profile local time from 24-hour format (e.g. “00:00”) to the 12-hour AM/PM style.
  6. // @author GooglyBlox
  7. // @match https://github.com/*
  8. // @match https://gist.github.com/*
  9. // @grant none
  10. // @run-at document-end
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. 'use strict';
  16.  
  17. function convertTo12Hour(timeStr) {
  18. const parts = timeStr.split(':');
  19. if (parts.length !== 2) return timeStr;
  20. let hours = parseInt(parts[0], 10);
  21. const minutes = parts[1];
  22. const suffix = hours >= 12 ? 'PM' : 'AM';
  23. hours = hours % 12;
  24. if (hours === 0) hours = 12;
  25. return hours + ':' + minutes + ' ' + suffix;
  26. }
  27.  
  28. function updateLocalTime() {
  29. const localTimeItem = document.querySelector('li[itemprop="localTime"]');
  30. if (!localTimeItem) return;
  31.  
  32. const labelEl = localTimeItem.querySelector('.p-label');
  33. if (!labelEl || !labelEl.firstChild) return;
  34.  
  35. if (labelEl.textContent.match(/\b(AM|PM)\b/)) return;
  36.  
  37. const timeRegex = /(\d{2}:\d{2})/;
  38. const originalText = labelEl.firstChild.textContent;
  39. const match = originalText.match(timeRegex);
  40. if (!match) return;
  41.  
  42. const originalTime = match[1];
  43. const convertedTime = convertTo12Hour(originalTime);
  44.  
  45. labelEl.firstChild.textContent = originalText.replace(originalTime, convertedTime);
  46. }
  47.  
  48. updateLocalTime();
  49.  
  50. const observer = new MutationObserver((mutations) => {
  51. mutations.forEach(() => {
  52. updateLocalTime();
  53. });
  54. });
  55.  
  56. observer.observe(document.body, { childList: true, subtree: true });
  57. })();