Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
$(document).ready(function(){
// Function to convert UTC time to visitor's timezone
function convertUTCtoVisitorTime(utcTime) {
const userTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const utcDate = new Date(utcTime + " UTC"); // Ensure 'utcTime' includes 'UTC' to avoid browser timezone offset
const visitorDate = utcDate.toLocaleString('en-GB', {
timeZone: userTimezone,
day: 'numeric',
month: 'short',
year: 'numeric'
}).replace(/(\w{3}) (\d{4})/, '$1, $2');
const visitorTime = utcDate.toLocaleString('en-GB', {
timeZone: userTimezone,
hour: 'numeric',
minute: 'numeric',
hour12: false
});
const timeZoneOffset = (utcDate.getTimezoneOffset() / 60) * -1
return { userTimezone, visitorDate, visitorTime, timeZoneOffset };
}
// Select all elements with class .convert--date
const dateElements = document.querySelectorAll('.convert--date');
// Loop through each element
dateElements.forEach(element => {
let dateStr = element.textContent.trim();
if ( dateStr == '' ) {
element.innerHTML = 'Date - TBD'
} else {
const { userTimezone, visitorDate, visitorTime, timeZoneOffset } = convertUTCtoVisitorTime(dateStr);
// Update the element content with visitor's time and timezone abbreviation
element.innerHTML = `${visitorDate} - <abbr title="${userTimezone} - UTC${timeZoneOffset > 0 ? '+' : ''}${timeZoneOffset}">${visitorTime}</abbr>`;
}
});
});