Template:Date table sorting/doc

From PhreakNet, the phreaks' encyclopedia
Revision as of 19:25, 10 August 2021 by Wikipedia>Timeshifter (→‎Abbreviation: typo)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Date table sorting is used to correctly sort and display dates in a sortable table (see Help:Sorting).

What it solves

Dates in their customary formats, if simply treated as alphanumeric text strings, sort in nonsensical ways:

  • 1 September 1998 sorts alphanumerically before 5 March 1967, because 1 is less than 5;
  • February 5, 1922 sorts alphanumerically before January 3, 1922, because F comes before J.

Use of the YYYY-MM-DD format would solve this problem, but with minor exceptions that format is not used in articles (see WP:DATEFORMAT).

This template automatically attaches a "hidden key" to dates, causing them to sort correctly while still being displayed in one of the customary display formats such as February 5, 1922 or 5 Feb 1922.

Parameters

Dates

The year, month and day can either be entered separately or as a single date.
{{Date table sorting|year|month|day|BC}} {{Date table sorting|date}}
  • 1st parameter – Year (optional if month is given)
    • Positive number (1 to 999,999,999,999) represent years AD or BC (dependant on the fourth parameter).
    • Negative numbers (−1 to −999,999,999,999) represent years BC (without the fourth parameter).
    • Year numbers must not contain commas or other formatting.
  • 2nd parameter – Month (optional if the day is not given either)
    • Full month name – e.g. February
    • Abbreviated month name – e.g. Feb
    • Month number – (either zero-padded or not) e.g. 2 or 02
  • 3rd parameter – Day (optional)
    • Day number – (either zero-padded or not) e.g. 7 or 07
  • 4th parameter – (optional)
    • May be BC, AD, BCE or CE
    • Used with a positive year number
  • 1st parameter – the full date
    • By default YYYY-MM-DD format becomes month-day-year format and DD-MM-YYYY format becomes day-month-year while other formats remain in the order they are input.
    • Abbreviated months are accepted and are abbreviated in the output.
    • Capitalisation and comma mistakes are corrected.
    • A single number is interpreted as a year, otherwise the date is parsed by the module. The following formats are accepted:
      • YYYY-MM-DD
      • DD-MM-YYYY
      • DD Month YYYY
      • Month DD, YYYY
      • YYYY-MM
      • DD Month
      • Month DD
      • Month YYYY
      • YYYY
      • Month
    • Day and month numbers may be 1 or 2 digits, and year numbers may only be 3 or 4 digits. If you need to input a number less than 1000, use leading zeroes (e.g. "0700").
    • BC years can only be parsed if the date is only the year number.
    • If you need to include the day or month for BC years or years greater than 9999, you must use separate parameters for year, month and day.


Examples
input style input code hidden sort key visible output
{{Date table sorting|year}} {{Date table sorting|1893}} 1893
{{Date table sorting|-89}} 89 BC
{{Date table sorting|-10000}} 10,000 BC
{{Date table sorting|-1}} 1 BC
{{Date table sorting| 1}} 1 (Note that year 0 does not exist and gives error message)
{{Date table sorting|month}} {{Date table sorting|Oct}} Oct
{{Date table sorting|month year}} {{Date table sorting|February 1767}} February 1767
{{Date table sorting|day month year}} {{Date table sorting|12 Aug 1987}} 12 Aug 1987
{{Date table sorting|month day, year}} {{Date table sorting|July 1, 1867}} July 1, 1867
{{Date table sorting|day month}} {{Date table sorting|25 October}} 25 October
{{Date table sorting|month day}} {{Date table sorting|Jan 1}} Jan 1
{{Date table sorting|YYYY-MM-DD}} {{Date table sorting|2014-12-09}} December 9, 2014
{{Date table sorting|DD-MM-YYYY}} {{Date table sorting|15-05-2019}} 15 May 2019
{{Date table sorting|year|month}} {{Date table sorting|1990|10}} October 1990
{{Date table sorting|1980|Dec}} Dec 1980
{{Date table sorting|1970|August}} August 1970
{{Date table sorting|year|month|day}} {{Date table sorting|2024|Jun|12}} Jun 12, 2024
{{Date table sorting|-204|11|22}} November 22, 204 BC
{{Date table sorting|year|month|day|era}} {{Date table sorting|44|3|15|BC}} March 15, 44 BC
{{Date table sorting|94|Oct|17|AD}} Oct 17, 94
{{Date table sorting|month}} {{Date table sorting||May}} May
{{Date table sorting|month|day}} {{Date table sorting||Sep|12}} Sep 12

Formatting

Using format produces a format that depends on, but is not necessarily equal to the format of the supplied date. The format parameter can take nine values.

Examples
value function input code hidden sort key visible output
  format depends on the input {{Date table sorting|26 Jan 1788}} 26 Jan 1788
{{Date table sorting|March 12, 1557}} March 12, 1557
{{Date table sorting|1975-02-13}} February 13, 1975
dmy displays the date in day-month-year format {{Date table sorting|Sep 13, 1888|format=dmy}} 13 Sep 1888
{{Date table sorting|1983-01-03|format=dmy}} 3 January 1983
dm displays only the day and month in day-month format {{Date table sorting|Feb 26, 1788|format=dm}} 26 Feb
{{Date table sorting|2020-10-15|format=dm}} 15 October
mdy displays the date in month-day-year format {{Date table sorting|August 31, 1901|format=mdy}} August 31, 1901
{{Date table sorting|2006-11-08|format=mdy}} November 8, 2006
md displays only the day and month in month-day format {{Date table sorting|26 Jun 1988|format=md}} Jun 26
{{Date table sorting|2002-02-27|format=md}} February 27
my displays only the month and year in month-year format {{Date table sorting|16 November 1488|format=my}} November 1488
d displays only the day {{Date table sorting|6 July 1666|format=d}} 6
m displays only the month {{Date table sorting|1980-02-12|format=m}} February
y displays only the year {{Date table sorting|-26|Jan|18|format=y}} 26 BC
hide hides the date {{Date table sorting|15 October 1800|format=hide}}

Abbreviation

There are two ways of abbreviating the month name.

  1. Directly input an abbreviated month name.
  2. Use the abbr=on parameter. It can be placed before or after the date in the template. This allows one to abbreviate the month later if the table becomes too wide. It is easy to use find-and-replace in a text editor, or the wikitext editor, to replace {{Date table sorting| with {{Date table sorting|abbr=on|
Examples
abbreviation method input code hidden sort key visible output
none {{Date table sorting|2000-01-01}} January 1, 2000
{{Date table sorting|2000|1|1}} January 1, 2000
{{Date table sorting|1 January 2000}} 1 January 2000
{{Date table sorting|2000|January|1}} January 1, 2000
abbreviated input {{Date table sorting|1 Jan 2000}} 1 Jan 2000
{{Date table sorting|2000|Jan|1}} Jan 1, 2000
abbr=on {{Date table sorting|1 January 2000|abbr=on}} 1 Jan 2000
{{Date table sorting|2000|January|1|abbr=on}} Jan 1, 2000
{{Date table sorting|2000-01-01|abbr=on}} Jan 1, 2000
{{Date table sorting|2000|1|1|abbr=on}} Jan 1, 2000

Additional sort key

In case two dates in a table are identical they may be ordered using the addkey parameter.

Examples
input code hidden sort key visible output
{{Date table sorting|2010-10-10|addkey=3}} October 10, 2010
{{Date table sorting|2010-10-09}} October 9, 2010
{{Date table sorting|2010-10-10|addkey=1}} October 10, 2010
{{Date table sorting|2010-10-10}} October 10, 2010
{{Date table sorting|2010-10-10|addkey=4}} October 10, 2010
{{Date table sorting|2010-10-11}} October 11, 2010
{{Date table sorting|2010-10-10|addkey=2}} October 10, 2010

Wrapping

To turn off the suppression of word wraps, use the nowrap=off parameter.

TemplateData

<templatedata> { "params": { "1": { "label": "Full date or year", "description": "Full date if second to fourth parameters are not specfied, otherwise year. Full date can be either in \"YYYY-MM-DD\", \"DD-MM-YYYY\", \"DD Month YYYY\", \"Month DD, YYYY\", \"YYYY-MM\", \"DD Month\", \"Month DD\", \"Month YYYY\" or \"Month\" format. Year is either a positive number or a negative number. If fourth parameter is not specified negative numbers are interpreted as years of BC era. Parameter is optional if the second parameter is specified.", "example": "\"1893\", \"-89\", \"-10000\", \"Oct\", \"February 1767\", \"12 Aug 1987\", \"July 1, 1867\", \"25 October\", \"Jan 1\", \"2014-12-09\"", "required": true, "type": "string" }, "2": { "label": "Month", "description": "Full or abbreviated month name or alternatively, a month number which can be zero-padded. Parameter is optional if the third parameter is not specified.", "example": "February, Feb, 2 or 02", "type": "string" }, "3": { "label": "Day", "description": "Day, optionally zero-padded", "example": "7 or 07", "type": "number" }, "4": { "label": "Era", "description": "BC, AD, BCE or CE. Takes effect only if the first parameter is a positive number.", "type": "string" }, "format": { "label": "Format", "description": "Controls the output format. Can be \"dmy\", \"dm\", \"mdy\", \"md\", \"my\", \"d\", \"m\", \"y\" or \"hide\".", "type": "string" }, "abbr": { "label": "Abbreviation", "description": "Abbreviates the month name", "example": "on", "type": "string", "default": "off" }, "addkey": { "label": "Additional sort key", "description": "Adds another sort key to resolve conflicts between rows with the same date", "example": "3", "type": "number" }, "nowrap": { "label": "Wrapping", "description": "To turn off the suppression of word wraps, use the \"off\" parameter", "example": "off", "type": "boolean", "default": "off" } }, "description": "Date table sorting is used to correctly sort and display dates in a sortable table." } </templatedata>

Notes

Sorting for incomplete dates

Whole months or years
A whole year or month (and year) is sorted according to its first day.
Missing years
If the year is not specified, the date will be sorted as if the year were the current year.
Blank dates
For no date input at all, the maximum possible sort key is used, so the entry will sort last.

Error handling

Errors are tracked in Category:Date table sorting templates with errors.

Calendar choice

The template is not capable of distinguishing Julian and Gregorian calendar dates. Also, it is not capable of taking a date in one calendar as input and outputting a date in the other calendar. Thus, to insure proper sorting, editors must either use one calendar for all dates, or pick a specific changeover date (for example, deciding that 4 October 1582 is Julian and all later dates are Gregorian); this would be done on an article-by article basis. Editors are also responsible for explaining to the reader whether the visible dates are Julian or Gregorian, if there is any possibility of confusion.

Linking

The template formerly linked dates automatically, with link=off used to suppress linking. In accordance with current guidelines linking of dates is no longer supported and instances of link=off should be removed.

See also

  • {{sort}}, sorting by a specified key (also good for complicated numbers, e.g. scientific notation)
  • {{sortname}}, full name sorting (using "last name, first name" as default sort key)
  • {{tts}}, for sorting times in result tables of athletic events (only needed in certain cases - see template documentation)
  • {{Number table sorting}}, for numbers