User:Dinoguy1000/scripts/ISO date format unifier.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. |
Documentation for this user script can be added at User:Dinoguy1000/scripts/ISO date format unifier. |
/******************************************************************************
* *
* ISO Date Format Unifier *
* (originally copied from *
* [[User:Remember the dot/ISO date format unifier.js]]) *
* *
* Thanks to [[User:GregU]] for pointing out a number of fixes and *
* improvements ([[User talk:GregU#Javascript assistance]]). *
* *
* This script reformats ISO dates (2000-01-15) into either MDY (January 15, *
* 2000) or DMY (15 January 2000) format. *
* *
* Known bugs: *
* - Ugly error reporting/handling (dies on error) *
* *
******************************************************************************/
function unifyIsoDateFormats(formattingAmericanStyle)
{
var formattingStyle = (formattingAmericanStyle) ? "MDY" : "DMY"; // try this
var wpTextbox1 = document.getElementById("wpTextbox1");
var wpSummary = document.getElementById("wpSummary");
var pageSource = wpTextbox1.value;
var pageSummary = wpSummary.value;
var newPageSummary = "converting ISO dates to " + formattingStyle + " per [[WP:MOSDATE]] ([[User:Dinoguy1000/scripts/ISO date format unifier.js|script-assisted]])";
var months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
var isoRegex = /(\[\[)?\b([12]\d\d\d)(\]\])?-(\[\[)?(0[1-9]|1[0-2])-([0-3]\d)\b(?!-)(\]\])?/g;
pageSource = pageSource.replace( isoRegex, function(date, a,year,b,c,month,day,d, pos,string)
{
// Don't touch dates inside links, template names, URLs, or HTML tags.
// Or if "date" ends a longer ID number like 33-2539-55-1987-10-25.
//
var pat = /\[\[[^|\]]*$|\{\{[^|}]*$|[:\/%][^\s|>]+$|<[^>]*$|-$/;
if (string.substring(pos-260,pos).search(pat) >= 0){
return date;
}
if (formattingAmericanStyle) {
return months[month-1] + " " + (day-0) + ", " + year; // day-0 removes leading zeros from the date; it should be left alone
}
else {
return (day-0) + " " + months[month-1] + " " + year;
}
});
if( pageSource == wpTextbox1.value ) {
alert("No ISO dates were found.");
return;
}
wpTextbox1.value = pageSource;
if( pageSummary.indexOf("ISO date format unifier.js") == -1 ) { // this needs to check if both buttons have been clicked,
if( pageSummary.match(/[^\*\/\s][^\/\s]?\s*$/) ){ pageSummary += "; "; } // and to update with the most recently clicked button
pageSummary += newPageSummary; // not sure the best way to do it, though
}
wpSummary.value = pageSummary;
document.getElementById("wpDiff").click();
}
function setUpIsoDateFormatUnifier()
{
document.getElementById("p-tb").getElementsByTagName("div")[0].getElementsByTagName("ul")[0].innerHTML += "<li><a href='javascript:unifyIsoDateFormats(true)'>Convert ISO dates to MDY (American)</a></li><li><a href='javascript:unifyIsoDateFormats(false)'>Convert ISO dates to DMY (international)</a></li>";
}
if (mw.config.get( "wgAction" ) == "edit" || mw.config.get( "wgAction" ) == "submit")
{
addOnloadHook(setUpIsoDateFormatUnifier);
}