Skip to content

Instantly share code, notes, and snippets.

@yogeshgosavi
Created April 29, 2025 06:29
Show Gist options
  • Save yogeshgosavi/06d7e67bf2b973a2329cf44ef4edf975 to your computer and use it in GitHub Desktop.
Save yogeshgosavi/06d7e67bf2b973a2329cf44ef4edf975 to your computer and use it in GitHub Desktop.
Converts 24-hour time format (e.g., "20:45") in `<strong>` elements on the IRCTC booking site to 12-hour format with AM/PM (e.g., "8:45 PM"). Injectable via browser console, with dynamic updates and robust parsing.
(function() {
function convertTo12HourFormat(timeString) {
// Clean the time string (e.g., "20:45" or "20:45:00")
const cleanedTime = timeString.trim().replace(/[^0-9:]/g, ''); // Keep only numbers and colons
const timeParts = cleanedTime.split(':');
const hours = parseInt(timeParts[0], 10);
const minutes = parseInt(timeParts[1], 10) || 0; // Fallback to 0 if minutes invalid
// Log for debugging
console.log('Parsed time:', { timeString, cleanedTime, hours, minutes });
// Convert to 12-hour format
const period = hours >= 12 ? 'PM' : 'AM';
const adjustedHours = hours % 12 || 12;
return `${adjustedHours}:${minutes.toString().padStart(2, '0')} ${period}`;
}
function updateTimeDisplay() {
// Select all <strong> elements
const timeElements = document.querySelectorAll('strong');
if (timeElements.length === 0) {
console.log('No <strong> elements found.');
return;
}
timeElements.forEach(element => {
const currentText = element.textContent.trim();
// Check if the text matches a time format (e.g., "20:45 | ")
if (/^\d{1,2}:\d{1,2}(?:\s*\|\s*)?$/.test(currentText)) {
// Extract the time part (before " | ")
const timePart = currentText.split(' | ')[0];
// Log for debugging
console.log('Processing element:', { currentText, timePart });
// Convert and update
const convertedTime = convertTo12HourFormat(timePart);
element.textContent = `${convertedTime} | `;
}
});
}
// Run immediately
updateTimeDisplay();
// Update every second
setInterval(updateTimeDisplay, 1000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment