User:Eraserhead1/date-fixer.js

function convertDates { var shouldBeInternational = confirm('OK for dd mmmm yyyy dates, cancel for mmmm dd, yyyy dates'); var text =$('div.wikiEditor-ui-text textarea').text; var accessdateMatches = getAllMatches(text, /\|accessdate=([^\|(\}\})]+)\|/); var dateMatches = getAllMatches(text, /\|date=([^\|(\}\})]+)\|/);

text = processDates(text, accessdateMatches, shouldBeInternational); text = processDates(text, dateMatches, shouldBeInternational);

$('div.wikiEditor-ui-text textarea').text(text); } $(function {    if ((wgAction == 'edit') && (wgNamespaceNumber == 0 || wgNamespaceNumber == 2)) {        mw.util.addPortletLink('p-tb', 'javascript:convertDates', 'Convert dates', 't-convert-dates', 'Convert dates to US or International format', , );    } });

function processDates(text, matches, shouldBeInternational){ for(var match in matches) { var dateString = match[1]; var date = Date.parse(dateString);

var resultDateString = (shouldBeInternational ? date.getDate + ' ' + date.getMonth + ' ': date.getMonth + ' ' + date.getDate + ', ') + date.getFullYear;

var initialLength = match[0].indexOf(dateString); text = text.substring(0, match.index + initialLength) + resultDateString + text.substring(match.index + initialLength + dateString.length); } }

function getAllMatches(text, regexString) { var matchArray = new Array; var currentMatch = null; while(true) { currentMatch = text.substring(currentMatch == null ? 0 : currentMatch.index + 1).match(regexString);

if(currentMatch == null) { break; }       matchArray.push(currentMatch); }

return matchArray; }