User:Hohum/datetools.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Hohum/datetools. |
// --------------------------------------- --------------------------------------- //
// ----------------------------------- 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;
}