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
 
(One intermediate revision by the same user not shown)
Line 25: Line 25:
     // Loop through each element
     // Loop through each element
dateElements.forEach(element => {
dateElements.forEach(element => {
const utcTime = element.textContent.trim(); // Assuming the content is in "YYYY-MM-DD HH:mm" UTC format
        let dateStr = element.textContent.trim();
const { userTimezone, visitorDate, visitorTime, timeZoneOffset } = convertUTCtoVisitorTime(utcTime);
        if ( dateStr == '' ) {
// Update the element content with visitor's time and timezone abbreviation
element.innerHTML = 'Date - TBD'
element.innerHTML = `${visitorDate} - <abbr title="${userTimezone} - UTC${timeZoneOffset > 0 ? '+'  : ''}${timeZoneOffset}">${visitorTime}</abbr>`;
        } 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>`;
        }
});
});
});
});

Latest revision as of 08:44, 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 = '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>`;
        }
	});
});