User:Remember the dot/Date format unifier.js

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: throw "Bad month!" } }

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

function unifyDateFormats(formattingAmericanStyle) {   var wpTextbox1 = document.getElementById("wpTextbox1") var pageSource = wpTextbox1.value var americanRegex = new RegExp("\\[\\[(January|February|March|April|May|June|July|August|September|October|November|December) ([0-3][0-9]|[0-9])\\]\\],? \\[\\[([0-9][0-9][0-9][0-9])\\]\\]") var britishRegex = new RegExp("\\[\\[([0-3][0-9]|[0-9]) (January|February|March|April|May|June|July|August|September|October|November|December)\\]\\],? \\[\\[([0-9][0-9][0-9][0-9])\\]\\]") var isoRegex = new RegExp("\\[\\[([0-9][0-9][0-9][0-9])-([0-1][0-9])-([0-3][0-9])\\]\\]") var lastAmericanRegexFoundAt = 0 var lastBritishRegexFoundAt = 0 var lastIsoRegexFoundAt = 0 var americanRegexResult = americanRegex.exec(pageSource) var britishRegexResult = britishRegex.exec(pageSource) var isoRegexResult = isoRegex.exec(pageSource) while (americanRegexResult || britishRegexResult || isoRegexResult) {       var reformattedDate if (americanRegexResult) {           if (formattingAmericanStyle) {               reformattedDate = americanRegexResult[1] + " " + americanRegexResult[2] + ", " + americanRegexResult[3] }           else {               reformattedDate = americanRegexResult[2] + " " + americanRegexResult[1] + " " + americanRegexResult[3] }           var regexFoundAt = pageSource.search(americanRegex, lastAmericanRegexFoundAt) if (regexFoundAt != -1) {               pageSource = pageSource.substr(0, regexFoundAt) + reformattedDate + pageSource.substr(regexFoundAt + americanRegexResult[0].length) americanRegexResult = americanRegex.exec(pageSource.substr(regexFoundAt + reformattedDate.length)) lastAmericanRegexFoundAt = regexFoundAt }           else {               americanRegexResult = null }       }        else if (britishRegexResult) {           if (formattingAmericanStyle) {               reformattedDate = britishRegexResult[2] + " " + britishRegexResult[1] + ", " + britishRegexResult[3] }           else {               reformattedDate = britishRegexResult[1] + " " + britishRegexResult[2] + " " + britishRegexResult[3] }           var regexFoundAt = pageSource.search(britishRegex, lastBritishRegexFoundAt) if (regexFoundAt != -1) {               pageSource = pageSource.substr(0, regexFoundAt) + reformattedDate + pageSource.substr(regexFoundAt + britishRegexResult[0].length) britishRegexResult = britishRegex.exec(pageSource.substr(regexFoundAt + reformattedDate.length)) }           else {               britishRegexResult = null }           lastBritishRegexFoundAt = regexFoundAt }       else if (isoRegexResult) {           if (formattingAmericanStyle) {               reformattedDate = monthStringFromMonthNumber(isoRegexResult[2]) + " " + removeLeadingZero(isoRegexResult[3]) + ", " + isoRegexResult[1] }           else {               reformattedDate = removeLeadingZero(isoRegexResult[3]) + " " + monthStringFromMonthNumber(isoRegexResult[2]) + " " + isoRegexResult[1] }           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 alert("Make sure that this function worked correctly!") }

function setUpDateFormatUnifier {   document.getElementById("p-tb").getElementsByTagName("div")[0].getElementsByTagName("ul")[0].innerHTML += "Format dates American styleFormat dates British style" }

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