User:Guywan/Scripts/12HourFormat.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Guywan/Scripts/12HourFormat. |
/*
* 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("wgAction") == "view"
|| mw.config.get("wgAction") == "edit"
|| mw.config.get("wgCanonicalNamespace") == "Special")
{
$(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 = partition($(this).text());
$(this).text(time[0] + time[1] + time[2]);
}
});
}
else if(mw.config.get("wgAction") == "edit")
{
if($("div").hasClass("mw-warning-with-logexcerpt mw-content-ltr"))
{
var html = $(".mw-logline-delete").html();
$(".mw-logline-delete").html(regexpReplace(html));
}
}
else if(mw.config.get("wgCanonicalNamespace") == "File")
{
// Modify time in file history table.
$("td > a").each(function()
{
var text = $(this).text();
var index = text.indexOf(":");
if(index > -1)
{
var time = text.substr(index - 2, 6);
// Will convert anything in the table matching the pattern "nn:nn," where n is a digit.
if(time.match(/\d\d:\d\d,/))
{
text = partition(text);
$(this).text(text[0] + text[1] + text[2]);
}
}
});
}
else if(mw.config.get("wgCanonicalNamespace") == "Special")
{
// Modify time when viewing user contributions.
if(mw.config.get("wgCanonicalSpecialPageName") == "Contributions")
{
$(".mw-contributions-list").children().each(function()
{
var text = partition($(this).children().first().text());
$(this).children().first().text(text[0] + text[1] + text[2]);
});
}
// Modify time when viewing user log info.
else if(mw.config.get("wgCanonicalSpecialPageName") == "Log")
{
$("ul").first().children().each(function()
{
var html = $(this).html();
$(this).html(regexpReplace(html));
});
}
// Modify time in user rights page.
else if(mw.config.get("wgCanonicalSpecialPageName") == "Userrights")
{
$("ul").children().each(function()
{
var html = $(this).html();
$(this).html(regexpReplace(html));
});
}
}
// Modify time when comparing two revisions.
else if((mw.config.get("wgAction") == "view") && (document.title.indexOf("Difference between revisions") > -1))
{
var comp = "#mw-diff-ntitle1 > strong > a";
var text = "";
for(i = 0; i < 2; i++)
{
text = partition($(comp).text());
$(comp).text(text[0] + text[1] + text[2]);
comp = "#mw-diff-otitle1 > strong > a";
}
text = partition($(".diff-currentversion-title").text());
$(".diff-currentversion-title").text(text[0] + text[1] + text[2]);
}
// Modify the time in the footer of most pages.
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);
var time = text.substr(text.indexOf(":") - 2, 5);
time = convert(time);
var end = text.substr(text.indexOf(":") + 3, text.length);
return new Array(front, time, end);
}
function regexpReplace(text)
{
var time = text.substr(1, 5);
return text.replace(/(\d\d\:\d\d)/g, convert(time));
}
function convert(time)
{
var hour = parseFloat(time.substr(0, 2));
if(hour > 12 && hour !== 0)
{
hour = hour - 12;
time = (hour + time.substr(2, 5) + " PM");
}
else
{
time = (hour + time.substr(2, 5) + " AM");
}
return time;
}