User:Remember the dot/ISO date format unifier.js

From Wikipedia, the free encyclopedia
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.
function monthStringFromMonthNumber(monthNumber)
{
    switch (monthNumber)
    {
        case "01":
            return "January"
        case "02":
            return "February"
        case "03":
            return "March"
        case "04":
            return "April"
        case "05":
            return "May"
        case "06":
            return "June"
        case "07":
            return "July"
        case "08":
            return "August"
        case "09":
            return "September"
        case "10":
            return "October"
        case "11":
            return "November"
        case "12":
            return "December"
        default:
        alert(monthNumber)
            throw "Bad month!"
    }
}

function removeLeadingZero(numberString)
{
    if (numberString.substr(0, 1) == "0")
    {
        return numberString.substr(1)
    }
    else
    {
        return numberString
    }
}

function unifyIsoDateFormats(formattingAmericanStyle)
{
    var wpTextbox1 = document.getElementById("wpTextbox1")
    var pageSource = wpTextbox1.value
    var isoRegex = /(\[\[)?([0-9][0-9][0-9][0-9])(\]\])?-(\[\[)?([0-1][0-9])-([0-3][0-9])(\]\])?/
    var lastIsoRegexFoundAt = 0
    var isoRegexResult = isoRegex.exec(pageSource)
    
    while (isoRegexResult)
    {
        if (formattingAmericanStyle)
        {
            reformattedDate = monthStringFromMonthNumber(isoRegexResult[5]) + " " + removeLeadingZero(isoRegexResult[6]) + ", " + isoRegexResult[2]
        }
        else
        {
            reformattedDate = removeLeadingZero(isoRegexResult[6]) + " " + monthStringFromMonthNumber(isoRegexResult[5]) + " " + isoRegexResult[2]
        }
        
        var regexFoundAt = pageSource.search(isoRegex, lastIsoRegexFoundAt)
        if (regexFoundAt != -1)
        {
            pageSource = pageSource.substr(0, regexFoundAt) + reformattedDate + pageSource.substr(regexFoundAt + isoRegexResult[0].length)
            isoRegexResult = isoRegex.exec(pageSource.substr(regexFoundAt + reformattedDate.length))
        }
        else
        {
            isoRegexResult = null
        }
        lastIsoRegexFoundAt = regexFoundAt
    }
    
    wpTextbox1.value = pageSource
    document.getElementById("wpDiff").click()
}

function setUpIsoDateFormatUnifier()
{
    document.getElementById("p-tb").getElementsByTagName("div")[0].getElementsByTagName("ul")[0].innerHTML += "<li><a href='javascript:unifyIsoDateFormats(true)'>Format ISO dates in American style</a></li><li><a href='javascript:unifyIsoDateFormats(false)'>Format ISO dates in international style</a></li>"
}

if (location.href.indexOf("action=edit") != -1 || location.href.indexOf("action=submit") != -1)
{
    addOnloadHook(setUpIsoDateFormatUnifier)
}