Jump to content

User:Guywan/Scripts/12HourFormat.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Guywan (talk | contribs) at 12:46, 24 January 2019 (Created page with '/* * Install with: * <code><nowiki>{{subst:Iusc|User:Guywan/Scripts/12HourFormat.js}}</nowiki></code> * or: * <code><nowiki>importScript("User:Guywan/Scripts...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
 * Install with:
 * <code><nowiki>{{subst:Iusc|User:Guywan/Scripts/12HourFormat.js}}</nowiki></code>
 * or:
 * <code><nowiki>importScript("User:Guywan/Scripts/12HourFormat.js"); // Backling: [[User:Guywan/Scripts/12HoursFormat.js]]</nowiki></code>
 *
 * This script was originally authored by User:Bility, [[User:Bility/convert24hourtime.js]],
 * and altered by User:DannyS712, [[User:DannyS712/12Hours.js]], whose version I have forked
 * to make some minor changes.
 */

var config =
{
	name 	: "[[User:Guywan/Scripts/12HourFormat]]",
	version : 3.0,
	debug	: false
};

if(mw.config.get("wgAction") == "history"
	|| mw.config.get("wgCanonicalNamespace") == "Special"
	|| ((mw.config.get("wgAction") == "view")
	&& (document.title.indexOf("Difference between revisions") > -1)))
{
	$(document).ready(function()
	{
		// Modify time when viewing page history.
		if(mw.config.get("wgAction") == "history")
		{
			$("span.mw-history-histlinks ~ a").each(function()
			{
				if($(this).attr("class") === "mw-changeslist-date")
				{
					var time = conver(this.html().substr(0, 5));
					
					this.html(converted + this.html().substr(5));
				}
			});
		}
		/*else if(mw.config.get("wgCanonicalNamespace") == "Special")
		{
			if(mw.config.get("wgCanonicalSpecialPageName") == "Contributions")
			{
				if($("div").hasClass("mw-warning-with-logexcerpt mw-content-ltr"))
				{
					do_log(true);
				}
				else
				{
					$("ul").first().children().each(function()
					{
						convertTo12HourTime($(this).children().first());
					});
				}
			}
		}*/
		// Modify time when comparing two revisions.
		else if(mw.config.get("wgAction") == "view")
		{
			var comp = "#mw-diff-ntitle1";
			var text = "";
			for(i = 0; i < 2; i++)
			{
				text = partition($(comp).text());
				
				console.log(text[0]);
				console.log(text[1]);
				console.log(text[2]);
				
				$(comp).text(text[0] + text[1] + text[2]);
				
				comp = "#mw-diff-otitle1";
			}
			
			text = partition($(".diff-currentversion-title").text());
			$(".diff-currentversion-title").text(text[0] + text[1] + text[2]);
		}
		
		// Modify the time that is in the footer of probably every page.
		if($("#footer-info-lastmod").length)
		{
			var footer = partition($("#footer-info-lastmod").text());
			
			$("#footer-info-lastmod").text(footer[0] + footer[1] + footer[2]);
		}
	});
}

function partition(text)
{
	var front = text.substr(0, text.indexOf(":") - 2);
	console.log(front);
	
	var time = text.substr(text.indexOf(":") - 2, 5);
	console.log(time);
	time = convert(time);
	console.log(time);
	
	var end = text.substr(text.indexOf(":") + 3, text.length);
	console.log(end);
	
	return new Array(front, time, end);
}

function convert(time)
{
	var hour = parseFloat(time.substr(0, 2));
	
	if(hour >= 12 && hour !== 0)
	{
		if(hour != 12)
		{
			hour = hour - 12;
		}
		
		time = (hour + time.substr(2, 5) + " pm");
	}
	else
	{
		time = (hour + time.substr(2, 5) + " am");
	}
	
	return time;
}