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"); // Backlink: [[User:Guywan/Scripts/12HoursFormat.js]]</nowiki></code>
*
* This script was originally authored by User:Bility, [[User:Bility/convert24hourtime.js]].
* Considerable contributions by User:DannyS712, [[User:DannyS712/12Hours.js]], whose version I have forked.
* I have mainly streamlined the code and introduced more functionality.
*/
$(function()
{
////////////////
// NAMESPACES //
////////////////
switch(mw.config.get("wgCanonicalNamespace"))
{
case "Special":
switch(mw.config.get("wgCanonicalSpecialPageName"))
{
case "Contributions":
$(".mw-contributions-list").children().each(function()
{
$(this).children().first().text(partition($(this).children().first().text()));
});
break;
case "Log":
case "Userrights":
$("ul").children().each(function()
{
$(this).html(regexpReplace($(this).html(), 1, 5));
});
break;
case "Project":
// TODO
break;
}
break;
case "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 a table matching the pattern "dd:dd," where d is a digit.
if(time.match(/\d\d:\d\d,/))
{
$(this).text(partition(text));
}
}
});
break;
case "User":
// TODO
break;
}
/////////////
// ACTIONS //
/////////////
switch(mw.config.get("wgAction"))
{
case "history":
$("span.mw-history-histlinks ~ a").each(function()
{
if($(this).attr("class") == "mw-changeslist-date")
{
$(this).text(partition($(this).text()));
}
});
break;
case "view":
if(document.title.indexOf("Difference between revisions") > -1)
{
var comp = "#mw-diff-ntitle1 > strong > a";
var temp;
for(i = 0; i < 2; i++)
{
temp = ($(comp).html());
$(comp).html(temp.replace(/(\d\d:\d\d)/g, convert(temp.substr(temp.indexOf(":") - 2, 5))));
comp = "#mw-diff-otitle1 > strong > a";
}
$(".diff-currentversion-title").text(partition($(".diff-currentversion-title").text()));
}
// Modify time in permalink pages.
if($("div").hasClass("#mw-revision"))
{
$("#mw-revision-date").html(regexpReplace($("#mw-revision-date").html(), 0, 5));
}
// Modify user signatures.
var html = $("#mw-content-text").html();
switch(mw.config.get("wgDefaultDateFormat"))
{
case "dmy":
// HH:MM, DD MM YY //
html = html.replace(/(<\/a>\)\s\d\d:\d\d,\s\d{1,2}\s\w+\s\d{4}\s)/g, partition);
break;
case "mdy":
// HH:MM, MM DD YY //
html = html.replace(/(<\/a>\)\s\d\d:\d\d,\s\w+\s\d{1,2}\s\d{4}\s)/g, partition);
break;
case "ymd":
// HH:MM, YY MM DD //
html = html.replace(/(<\/a>\)\s\d\d:\d\d,\s\d{4}\s\w+\s\d{1,2}\s)/g, partition);
break;
}
$("#mw-content-text").html(html);
break;
case "edit":
// TODO
break;
}
///////////
// OTHER //
///////////
// Modify time in warning banners.
if($("div").hasClass("mw-warning-with-logexcerpt mw-content-ltr"))
{
$(".mw-warning-with-logexcerpt.mw-content-ltr > ul").children().each(function()
{
$(this).html(regexpReplace($(this).html(), 1, 5));
});
}
// Modify the time in the footer of most pages.
if($("#footer-info-lastmod").length)
{
$("#footer-info-lastmod").text(partition($("#footer-info-lastmod").text()));
}
});
function partition(text)
{
var front = text.substr(0, text.indexOf(":") - 2);
var time = convert(text.substr(text.indexOf(":") - 2, 5));
var end = text.substr(text.indexOf(":") + 3, text.length);
return (front + time + end);
}
function regexpReplace(html, start, num)
{
return html.replace(/(\d\d:\d\d)/g, convert(html.substr(start, num)));
}
function convert(time)
{
var hour = parseFloat(time.substr(0, 2));
if(hour >= 12)
{
if(hour != 12)
{
hour = hour - 12;
}
return (hour + time.substr(2, 5) + " PM");
}
if(hour === 0)
{
hour = 12;
}
return (hour + time.substr(2, 5) + " AM");
}