MediaWiki:DateTimezones.js: Difference between revisions
From TwogPedia
(Created page with "$(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{...") |
mNo edit summary |
||
Line 25: | Line 25: | ||
// Loop through each element | // Loop through each element | ||
dateElements.forEach(element => { | dateElements.forEach(element => { | ||
let dateStr = element.textContent.trim(); | |||
if ( dateStr == '' ) { | |||
element.innerHTML = '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>`; | |||
} | |||
}); | }); | ||
}); | }); |
Revision as of 08:43, 15 October 2023
$(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 = '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>`;
}
});
});
No categories