Jump to content

User:RossPatterson/ISO8601 to mmm dd yyyy.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.
/* Add a tab called "de-iso8601" to convert yyyy-mm-dd dates to mmm dd, yyyy */

/* NOTE: This script is agressive.  It doesn't try to avoid any changes - if it finds 
         a string in "nnnn-nn-nn" format, it will attempt to convert it.  You are
         well-advised to examine the diffs closely, and to especially look for
         partially-successful conversions (i.e., "_FiXmE_..._fIxMe") and dates buried
         in URLs (e.g., ".../2007-11-05_article.html" -> ".../November 5, 2007_article.html"
         and correct them by hand!

Inspired by [[User:MJCdetroitMJCdetroit's]] Units of measure formatting and conversion tool and date formatting tool ([[User:MJCdetroit/monobook.js]])

Simply add ''importScript("User:RossPatterson/ISO8601_to_mmm_dd_yyyy.js");'' to your own monobook.js and clear the cache before it will work.
*/
 
function ISO8601_to_mmm_dd_yyyy() {
    var txt = document.editform.wpTextbox1;

    // Find and mark yyyy-mm-dd dates to be changed:
    txt.value = txt.value.replace(/(\d\d\d\d-\d\d-\d\d)/g, '_FiXmE_$1_fIxMe_');
    // [[yyyy-mm-dd]] -> yyyy-mm-dd:
    txt.value = txt.value.replace(/\[\[_FiXmE_(\d\d\d\d-\d\d-\d\d)_fIxMe_\]\]/g, '_FiXmE_$1_fIxMe_'); 
    // yyyy-mm-dd -> mm dd, yyyy:
    txt.value = txt.value.replace(/_FiXmE_(\d\d\d\d)-(\d\d)-(\d\d)_fIxMe_/g, '_FiXmE_$2 $3, $1_fIxMe_'); 
    // 01, ... -> January, ... etc.:
    txt.value = txt.value.replace(/_FiXmE_01 (\d\d, \d\d\d\d)_fIxMe_/g, '_FiXmE_January $1_fIxMe_'); 
    txt.value = txt.value.replace(/_FiXmE_02 (\d\d, \d\d\d\d)_fIxMe_/g, '_FiXmE_February $1_fIxMe_'); 
    txt.value = txt.value.replace(/_FiXmE_03 (\d\d, \d\d\d\d)_fIxMe_/g, '_FiXmE_March $1_fIxMe_'); 
    txt.value = txt.value.replace(/_FiXmE_04 (\d\d, \d\d\d\d)_fIxMe_/g, '_FiXmE_April $1_fIxMe_'); 
    txt.value = txt.value.replace(/_FiXmE_05 (\d\d, \d\d\d\d)_fIxMe_/g, '_FiXmE_May $1_fIxMe_'); 
    txt.value = txt.value.replace(/_FiXmE_06 (\d\d, \d\d\d\d)_fIxMe_/g, '_FiXmE_June $1_fIxMe_'); 
    txt.value = txt.value.replace(/_FiXmE_07 (\d\d, \d\d\d\d)_fIxMe_/g, '_FiXmE_July $1_fIxMe_'); 
    txt.value = txt.value.replace(/_FiXmE_08 (\d\d, \d\d\d\d)_fIxMe_/g, '_FiXmE_August $1_fIxMe_'); 
    txt.value = txt.value.replace(/_FiXmE_09 (\d\d, \d\d\d\d)_fIxMe_/g, '_FiXmE_September $1_fIxMe_'); 
    txt.value = txt.value.replace(/_FiXmE_10 (\d\d, \d\d\d\d)_fIxMe_/g, '_FiXmE_October $1_fIxMe_'); 
    txt.value = txt.value.replace(/_FiXmE_11 (\d\d, \d\d\d\d)_fIxMe_/g, '_FiXmE_November $1_fIxMe_'); 
    txt.value = txt.value.replace(/_FiXmE_12 (\d\d, \d\d\d\d)_fIxMe_/g, '_FiXmE_December $1_fIxMe_'); 
    // mmm 0d, yyyy -> mmm d, yyyy:
    txt.value = txt.value.replace(/_FiXmE_(\w*) 0(\d, \d\d\d\d)_fIxMe_/g, '_FiXmE_$1 $2_fIxMe_'); 
    // Unmark changed dates:
    txt.value = txt.value.replace(/_FiXmE_(\w* \d{1,2}, \d\d\d\d)_fIxMe_/g, '$1'); 

}
 
function run_ISO8601_to_mmm_dd_yyyy() {
    var txt = document.editform.wpTextbox1;
    ISO8601_to_mmm_dd_yyyy();
 
    // Add a tag to the summary box
    var txt = document.editform.wpSummary;
    var summary = "Change ISO8601-style dates to mmm dd, yyyy and unlink them";
	if (txt.value.indexOf(summary) == -1) {
		if (txt.value.match(/[^\*\/\s][^\/\s]?\s*$/)) {
			txt.value += " | ";
		}
		txt.value += summary;
	}
 
    // Press the diff button to check it
    document.editform.wpDiff.click()
}

addOnloadHook(function () {
    if(document.forms.editform) {
        mw.util.addPortletLink('p-cactions', 'javascript:run_ISO8601_to_mmm_dd_yyyy()', 'De-ISO8601', 'ca-de-iso8601', 'Change ISO8601-style dates to mmm dd, yyyy"', '', '');
    }
});