Jump to content

User:Plastikspork/datetools.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.
// ---------------------------------------  --------------------------------------- //
// ----------------------------------- Credits ------------------------------------ //
//
// These javascript tools were inspired by
//      [[User:Lightmouse/monobook.js/script.js]]
//
// This script is intended to be complimentary to these scripts with very little
// to no duplication of function.
 
// ---------------------------------- Disclaimer ---------------------------------- //
//
// Use at your own risk and make sure you check the edit changes before you save
//
// Let me know [[User_Talk:Plastikspork]] if you find bugs!

// ----------------------------- Installing the Script ---------------------------- //
//
// (1) Open/Create your USERNAME/monobook.js page, where USERNAME is your username.
//     A quick way to get there is to go to your user page, then append
//     '/monobook.js' to the end of the URL.
//
// (2) Put the following command on your monobook.js page:
//          importScript('User:Plastikspork/datetools.js');
//
// (3) Save the page and reload it by following the instructions at the top of your
//     monobook.js page.  For example, Ctrl+Shift+R in Firefox.

// ------------------------------- Using the Script ------------------------------- //
//
// (1) This is a collection of javascript functions which can be called from other
//     scripts.
//
//  Note: Some functions could run slowly on very large pages, but do 
//        always eventually complete in my experience.
// ---------------------------------------  --------------------------------------- //

// ---------------------------- spork_yyyymmdd_to_mdy ----------------------------- //
// Purpose: Changes/delinks yyyy-mm-dd to Month dd, yyyy if preceded by 'date ='
//
// Examples: 'date = 1990-12-20' -> 'date = December 12, 1990'
//           'accessdate = [[1990-12-20]]' -> 'accessdate = December 12, 1990'
//
// Notes: Currently there is an injunction against mass delinking of dates
//
function spork_yyyymmdd_to_mdy(str) {
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)0?1-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1January $3, $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)0?2-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1February $3, $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)0?3-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1March $3, $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)0?4-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1April $3, $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)0?5-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1May $3, $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)0?6-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1June $3, $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)0?7-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1July $3, $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)0?8-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1August $3, $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)0?9-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1September $3, $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)10-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1October $3, $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)11-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1November $3, $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)12-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1December $3, $2');
  return str;
}

// ---------------------------- spork_yyyymmdd_to_dmy ----------------------------- //
// Purpose: Changes/delinks yyyy-mm-dd to dd Month yyyy if preceded by 'date ='
//
// Examples: 'date = 1990-12-20' -> 'date = 12 December 1990'
//           'accessdate = [[1990-12-20]]' -> 'accessdate = 12 December 1990'
//
// Notes: Currently there is an injunction against mass delinking of dates
//
function spork_yyyymmdd_to_dmy(str) {
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)0?1-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1$3 January $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)0?2-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1$3 February $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)0?3-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1$3 March $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)0?4-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1$3 April $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)0?5-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1$3 May $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)0?6-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1$3 June $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)0?7-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1$3 July $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)0?8-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1$3 August $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)0?9-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1$3 September $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)10-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1$3 October $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)11-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1$3 November $2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?([1-9][0-9]{3})(?:\]\]-\[\[|-)12-(0[1-9]|[1-3][0-9])(?:\]\])?/gi, '$1$3 December $2');
  return str;
}

// ------------------------------- spork_dmy_to_mdy ------------------------------- //
// Purpose: Changes/delinks DD Month YYYY to Month DD, YYYY if preceded by 'date ='
//
// Examples: 'accessdate = 12 December 1990' -> 'accessdate = December 12, 1990'
//           'date = [[12 December]] [[1990]]' -> 'date = December 12, 1990'
//
// Notes: Currently there is an injunction against mass delinking of dates
//
function spork_dmy_to_mdy(str) {
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?0?([1-9]|[1-3][0-9])[ ,]+((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*)(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3 $2, $4');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*)[, ]+0?([1-9]|[1-3][0-9])(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$2 $3, $4');
  return str;
}

// ------------------------------- spork_mdy_to_dmy ------------------------------- //
// Purpose: Changes/delinks Month DD, YYYY to DD Month YYYY if preceded by 'date ='
//
// Examples: 'accessdate = December 12, 1990' -> 'accessdate = 12 December 1990'
//           'date = [[December 12]], [[1990]]' -> 'date = 12 December 1990'
//
// Notes: Currently there is an injunction against mass delinking of dates
//
function spork_mdy_to_dmy(str) {
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*)[, ]+0?([1-9]|[1-3][0-9])(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3 $2 $4');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?0?([1-9]|[1-3][0-9])[, ]+((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*)(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$2 $3 $4');
  return str;
}

// ---------------------------- spork_dmy_to_yyyymmdd ----------------------------- //
// Purpose: Changes/delinks dd Month yyyy to yyyy-mm-dd if preceded by 'date ='
//
// Examples: 'date = 12 December 1990' -> 'date = 1990-12-20'
//           'accessdate = [[12 December]] [[1990]]' -> 'accessdate = 1990-12-20'
//
// Notes: Currently there is an injunction against mass delinking of dates
//
function spork_dmy_to_yyyymmdd(str){
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?0?([1-9]|[1-3][0-9]) Jan[\.uary]*(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-01-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?0?([1-9]|[1-3][0-9]) Feb[\.uary]*(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-02-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?0?([1-9]|[1-3][0-9]) Mar[\.ch]*(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-03-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?0?([1-9]|[1-3][0-9]) Apr[\.il]*(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-04-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?0?([1-9]|[1-3][0-9]) May(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-05-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?0?([1-9]|[1-3][0-9]) Jun[\.e]*(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-06-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?0?([1-9]|[1-3][0-9]) Jul[\.y]*(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-07-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?0?([1-9]|[1-3][0-9]) Aug[\.ust]*(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-08-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?0?([1-9]|[1-3][0-9]) Sep[\.tembr]*(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-09-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?0?([1-9]|[1-3][0-9]) Oct[\.ober]*(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-10-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?0?([1-9]|[1-3][0-9]) Nov[\.embr]*(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-11-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?0?([1-9]|[1-3][0-9]) Dec[\.embr]*(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-12-$2');

  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*[1-9][0-9][0-9][0-9])-([1-9]-)/gi, '$1-0$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*[1-9][0-9][0-9][0-9]-[0-9][0-9])-([1-9])([^0-9])/gi, '$1-0$2$3');
  return str;
}

// ---------------------------- spork_mdy_to_yyyymmdd ----------------------------- //
// Purpose: Changes/delinks Month dd, yyyy to yyyy-mm-dd if preceded by 'date ='
//
// Examples: 'date = December 12, 1990' -> 'date = 1990-12-20'
//           'accessdate = [[December 12]], [[1990]]' -> 'accessdate = 1990-12-20'
//
// Notes: Currently there is an injunction against mass delinking of dates
//
function spork_mdy_to_yyyymmdd(str){
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?Jan[\.uary]* 0?([1-9]|[1-3][0-9])(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-01-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?Feb[\.uary]* 0?([1-9]|[1-3][0-9])(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-02-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?Mar[\.ch]* 0?([1-9]|[1-3][0-9])(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-03-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?Apr[\.il]* 0?([1-9]|[1-3][0-9])(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-04-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?May 0?([1-9]|[1-3][0-9])(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-05-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?Jun[\.e]* 0?([1-9]|[1-3][0-9])(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-06-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?Jul[\.y]* 0?([1-9]|[1-3][0-9])(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-07-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?Aug[\.ust]* 0?([1-9]|[1-3][0-9])(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-08-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?Sep[\.tembr]* 0?([1-9]|[1-3][0-9])(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-09-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?Oct[\.ober]* 0?([1-9]|[1-3][0-9])(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-10-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?Nov[\.embr]* 0?([1-9]|[1-3][0-9])(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-11-$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*)(?:\[\[)?Dec[\.embr]* 0?([1-9]|[1-3][0-9])(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-12-$2');

  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*[1-9][0-9][0-9][0-9])-([1-9]-)/gi, '$1-0$2');
  str=str.replace(/(\|[ ]*(?:date|accessdate|archivedate)[ ]*=[ ]*[1-9][0-9][0-9][0-9]-[0-9][0-9])-([1-9])([^0-9])/gi, '$1-0$2$3');
  return str;
}