User:Hohum/datetools.js

Source: 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:Plastikspork/datetools.js]]
//

 
// ---------------------------- spork_dmy_to_yyyymmdd ----------------------------- //
// Purpose: Changes/delinks dd Month yyyy to yyyy-mm-dd if preceded by ANYTHING '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(/(?:\[\[)?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(/(?:\[\[)?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(/(?:\[\[)?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(/(?:\[\[)?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(/(?:\[\[)?0?([1-9]|[1-3][0-9]) May(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-05-$2');
  str=str.replace(/(?:\[\[)?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(/(?:\[\[)?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(/(?:\[\[)?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(/(?:\[\[)?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(/(?:\[\[)?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(/(?:\[\[)?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(/(?:\[\[)?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(/(\|[ ]*[1-9][0-9][0-9][0-9])-([1-9]-)/gi, '$1-0$2');
  str=str.replace(/(\|[ ]*[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(/(?:\[\[)?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(/(?:\[\[)?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(/(?:\[\[)?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(/(?:\[\[)?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(/(?:\[\[)?May 0?([1-9]|[1-3][0-9])(?:\]\])?[, ]+(?:\[\[)?([1-9][0-9][0-9][0-9])(?:\]\])?/gi, '$1$3-05-$2');
  str=str.replace(/(?:\[\[)?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(/(?:\[\[)?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(/(?:\[\[)?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(/(?:\[\[)?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(/(?:\[\[)?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(/(?:\[\[)?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(/(?:\[\[)?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(/(\|[ ]*[1-9][0-9][0-9][0-9])-([1-9]-)/gi, '$1-0$2');
  str=str.replace(/(\|[ ]*[1-9][0-9][0-9][0-9]-[0-9][0-9])-([1-9])([^0-9])/gi, '$1-0$2$3');
  return str;
}