User:Ohconfucius/script/MOSNUM dates

Overview
This script delinks all dates and date fragments, and converts them all to the same format, either dd mmm yyyy ("14 July 1978") or mmm dd, yyyy ("July 14, 1978").

MOSNUM allows one other format to be used in articles – but only in tables and reference sections. NOTE: Functions converting dates within citation templates has been withdrawn now that MW software engine renders date formats automatically There are modes that allow the user to select whether to treat body dates only or body and reference sections:
 * 1) all dates to dmy – dates in the body of the text as well as in the reference sections are converted to "14 July 1978" or similar.
 * 2) all dates to mdy – as above. dates are converted to "July 14, 1978" or similar.
 * 3)  Big endian ref dates  button now disabled. Users are invited to manually add cs1-dates parameter to Use dmy dates or Use mdy dates causing applicable dates to be rendered in iso YMD format

In addition, there are the following modes to convert slash or contracted dates into the desired format: *Note: In these latter 2 cases, false positives are more likely because the protection mechanism of the script is bypassed, meaning the regex will act on dates in instances other than where the date parameter is used
 * 1) US-slash dates – converts dates that use slash or decimal separators in formats commonly used in the US into mdy dates viz:,  ,   or
 * 2) UK-slash dates – converts dates that use slash or decimal separators in formats commonly used in the UK, Europe and elsewhere into dmy dates viz: ,  ,   or

History
Lightmouse initially created a script to deal with a number of general formatting issues, and date alignment was one such function. I used it and often gave feedback and bug reports. Seeing the need to work on the diversity of date formats and date situations that are seen here on Wikipedia, I created an independent script targeted at dates with some help and advice from Lightmouse.

Over the years, this script has been built on and refined. In April 2012, the engine underwent a major rework by, who built it and incorporated a new regex_worker function that allows more effective treatment of date fragments; there is better protection of strings that ought not to be modified by the script. In May, the new improved version of the script went 'live'.

Detailed functional overview
The dates script is at User:Ohconfucius/script/MOSNUM dates.js.
 * It is aimed to make displayed dates fully compatible with Manual of Style (dates and numbers) (WP:MOSNUM)
 * It ensures uniform presentation of dates within any given article. As such, it:
 * delinks all dates and date fragments, including day-month strings, days, months, decades, centuries,
 * converts all these date fragments to the same format, either dd mmm yyyy or mmm dd, yyyy, at the option of the user
 * removes ordinal suffixes and constructions such as ' the 5 th of September', 'December 25 th ' or 'October of 2003'
 * adds commas where necessary (e.g. February 28 2001)
 * removes redundant commas (e.g. July, 1997; 28 February , 2001)
 * It removes direct links to dates, and most form of piped date links
 * It removes year linking templates (such as scy, by), and links to 'year-in X' where piped from years (such as 1984 )
 * It converts other often used (but not MOSNUM-compliant) date formats within the references section, such as dd-mm-yyyy or dd-mmm-yyyy to the chosen prevailing format used in the rest of the article.
 * It makes certain fixes that may be identified as CS1 dates errors
 * It also allows users to choose to convert dates within only the body of the article, as is permitted for reference sections (where yyyy-mm-dd dates are common) to have different dates, or throughout the article. Now disabled. CS1 now manages the date rendering by auto-formatting citation template dates using parameters within the use dmy dates and use mdy dates templates, so this can be used to fine-tune the rendering.

The script adds or updates a non-displaying template use mdy dates or use dmy dates depending on the conversion used. This tagging allows for WM software engine to render date formats automatically in conformity with tagged format). It also facilitates future maintenance by bot by script AWB.

The script will achieve overall consistency of the format of the date. The format and syntax of citations were modelled after the way reference sections are rendered when using the most common citation templates – cite news and cite web; I try to stick to syntax in where 'Retrieved' is capitalised and preceded by a full-stop.

Although the MediaWiki software interprets use dmy dates or use mdy dates templates, allowing dates and access dates within citation templates to be automatically formatted, errors there and elsewhere may still require correction. The script takes care of that.

How it works
The script is configured so that dates are stored as an array. The given regex and substitution strings can contain magic words that specify what date format the routine should accept and to what date format the routine should convert the date to.

There is a mechanism in place to protect a wide range of strings that ought to be immutable: such as images, categories, urls; text within blockquotes and double quotes; text within certain parameters (e.g. ). Occasionally, due to the proliferation of templates across en.wp, some may be the subject of unintended changes that have not been anticipated in the script regexes.

As also mentioned below, the actions of the protection mechanism can be revealed in the event of the script stalling, and substitution markers may appear in the edit window. When this occurs, the user should abandon the action, reopen the edit window and reperform the edit.

Optional auto-formatting citation template dates
Script default is without parameter, in which case date is aligned to the prevailing style, dmy or mdy.

Citation Style 1 and 2 (collectively cs1|2) templates automatically render dates (date, access-date, archive-date, etc) in the style specified by this template. Because it is allowed by WP:MOSDATES, the cs1|2 templates can automatically format dates in a variety of styles. Editors may choose how cs1|2 templates render dates by the use of &lt;keyword>.

An example is given at.

Credits
I am solely responsible for this tool, which has been inspired by the dates scripts written by User:Lightmouse and User:Plastikspork. It incorporates many additional features, such as unlinking many unusually-piped date-links, conversion of date series and ranges, and delinking months of the year. Thanks also to User:1exec1 for the invaluable assistance in rewriting and simplifying the workings of the script in April 2012.

Feedback is appreciated at User Talk:Ohconfucius. Please report any false negatives as well as false positives you may detect.

Actions and test
Use of the Safari or Firefox browsers is highly recommended. It only functions in desktop mode.

The script is continually being run, and seems to work as intended on the latest Firefox and Chrome browsers on Mac and Windows. On Windows machines, browser-specific issues for IE, specifically version 8 have been reported. These may or may not have been resolved.

The tool may also be used on mobile browsers. However, extensive testing has not been undertaken.

Once you are in edit mode, there are buttons in the toolbox in the left margin:
 * 1) DATES to dmy – converts all dates to dmy dmy format
 * 2) DATES to mdy – converts all dates to mdy mdy format
 * 3) US-slash dates – converts all applicable dates into mdy mdy format
 * 4) UK-slash dates – converts all applicable dates into dmy dmy format


 * Notes

Users are equally reminded of the following provisions of Wikipedia:Manual of Style (dates and numbers) :

Format consistency

 * Dates in article body text should all use the same format: Julia ate a poisoned apple on 25 June 2005, and died on 28 June (not ... on June 28).
 * Publication dates in references should all use the same format. Any format from the "Acceptable date formats" table above may be used, unless the citation style being used requires a different format (however, all-numeric date formats other than yyyy-mm-dd must still be avoided).
 * Access and archive dates in references should all use the same format – either the format used for publication dates, or yyyy-mm-dd

(These consistency requirements apply to dates in general prose and reference citations, but not to dates in quotations or titles)

Strong national ties to a topic

 * Articles on topics with strong ties to a particular English-speaking country should generally use the more common date format for that nation. For the United States, this is month before day; for most others, it is day before month. Articles related to Canada may use either format consistently.
 * Sometimes the customary format differs from the usual national one: for example, articles on the modern US military use day before month, in accordance with military usage.

Retaining the existing format

 * If an article has evolved using predominantly one format, the whole article should conform to it, unless there are reasons for changing it based on strong national ties to the topic or consensus on article talk.
 * The date format chosen by the first major contributor in the early stages of an article should continue to be used, unless there is reason to change it based on strong national ties to the topic or consensus on article talk.
 * Where an article has shown no clear sign of which format is used, the first person to insert a date is equivalent to "the first major contributor".

Test pages

 * User:Full-date unlinking bot/Test environment - tests unlinking and dmy<–>mdy conversion
 * User:Dl2000/testtemp7 - slash dates
 * User:Ohconfucius/MOSNUM script test – test page dealing with CS1 date errors

Known limitations

 * Proper nouns with dates: A number of proper nouns containing date fragments (such as 'March 14 Alliance', '6th of October City', 'June 1, 1974', '2000 AD') have been specifically protected from conversion or delinking. Only the displayed part of piped links are acted upon, whilst the underlying pipe will not be changed. For the purposes of this script, September 11 or September 11 attacks is treated: there are ample redirects in existence for any problems to be mitigated. Let me know if there are other proper names which ought to be protected.
 * Dates that are used as adjectives: Because the script routinely removes the leading "the" for dates, attention should be paid to where the date is used as an adjective, such as " the 6 June beach landings". The "the" should either be reinserted, or the phrase reconfigured so that the date is no longer adjectival (e.g. "the beach landings on 6 June").


 * Stalling: If you see characters such as the following in your diffs window, don't panic:
 * this is simply the result of a stalling of the script, where the protected strings haven't been restored by the time you see the changes displayed on screen. It happens occasionally. In such a case, you should abandon the edit by refreshing the edit window, and then click on the script button again.
 * this is simply the result of a stalling of the script, where the protected strings haven't been restored by the time you see the changes displayed on screen. It happens occasionally. In such a case, you should abandon the edit by refreshing the edit window, and then click on the script button again.
 * this is simply the result of a stalling of the script, where the protected strings haven't been restored by the time you see the changes displayed on screen. It happens occasionally. In such a case, you should abandon the edit by refreshing the edit window, and then click on the script button again.

Other Wiki scripts or gadgets

 * wikEd: The script is known to conflict with wikEd that prevents the MOSNUM dates script from making any changes to articles, or simply blocking the creation of a script button. The solution is to toggle the wikEd text area off while running MOSNUM dates. wikEd has an easy toggle button for that called: Use wikEd instead of classic text area. Simply press the toggle button so the button pops out (off), then run MOSNUM dates. When you are done running MOSNUM dates you can press the toggle button again so the toggle button pops in (on) and the wikEd text area will be live again. Once you get in the habit of toggling the wikEd text area on and off, running MOSNUM dates is no longer a conflict with wikEd. There is no need to disable/re-enable wikEd in your common.js file.

Browsers

 * It has been reported that Chrome extension "Privacy Badger" may somehow inhibit the script functioning.

Disclaimers
I use and make experimental changes to another version, and update this production version regularly to a suitably stable prior version of the test script. However, like all software, bugs are unavoidable. Use at your own risk, and make sure you check the edit changes before you save. The script is continually being run, and seems to work as intended on the latest Firefox and Chrome browsers. However, browser-specific issues have been reported for IE on Windows machines in the past (as at 1 January 2019, there are no recent reports of such compatibility issues).

By consensus, this script should not be used in articles about chronological periods, such as 2015 or January 25, as date links here, considered desirable, would otherwise be automatically removed. Equally, I suspect that using the script on talk pages or on WP pages is likely to result in more angst than benefit to the project.