User:Dinoguy1000/scripts/ISO date format unifier.js

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.
/******************************************************************************
 *                                                                            *
 * 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);
}