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{...") |
(No difference)
|
Revision as of 18:09, 9 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 => {
const utcTime = element.textContent.trim(); // Assuming the content is in "YYYY-MM-DD HH:mm" UTC format
const { userTimezone, visitorDate, visitorTime, timeZoneOffset } = convertUTCtoVisitorTime(utcTime);
// 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