模板:Date/doc

来自决策链云智库
< 模板:Date
Zeroclanzhang讨论 | 贡献2024年1月17日 (三) 16:56的版本 (创建页面,内容为“{{Documentation subpage}} <!-- Place categories where indicated at the bottom of this page and interwikis at Wikidata --> {{for|dates formatted with the #time parser function|mw:Help:Extension:ParserFunctions#.23time}} {{High-use| 40880 }} ===Description=== This template converts a date to one of the standard date formats used on the English Wikipedia. '''This template should only be used internally in other templates.''' If you want sortable dates for a tab…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

Description

This template converts a date to one of the standard date formats used on the English Wikipedia.

This template should only be used internally in other templates.

If you want sortable dates for a table, use {{date table sorting}} instead.

Syntax

This template takes up to two parameters, both of which are optional:

{{date|<date to be formatted>|<date formatting style>}}

  • <date to be formatted> is an (optional) date to be formatted.
    If no date is specified, the template emits the current date.
    For example, {{date}} produces 22 11月 2024. To avoid emitting the current date if none is input, use Template:fdate.
    If a date is provided, but is not recognized as one, the text is just returned as is. Most dates will however be recognized as such, including all the formats typically used on Wikipedia. Numerous variations thereof are also supported, including abbreviated or lower-case month names; ordinal "st", "nd", "rd", "th"; extraneous white space; missing comma, etc.
    Notes:
    • Some dates are converted even if they are not real dates, and some dates are not converted even though they are real dates. See known issues below.
    • Only dates within the range 1 1月 1000 and 31 12月 9999 are supported. Dates with two-digit years will have the years from 1970 to 2069. Other dates outside the range 1000–9999 will be treated as if the second parameter were "none".
    • when using month numbers in numeric, the valid formats are MM/DD/YYYY, DD-MM-YYYY, DD.MM.YYYY, YYYY/MM/DD, YYYY-MM-DD, MM/DD/YY, YY-MM-DD, DD.MM.YY, the invalid formats are DD/MM/YYYY, MM-DD-YYYY, MM.DD.YY, YYYY.MM.DD, MM.DD.YYYY, DD-MM-YY, YY/MM/DD, DD/MM/YY, YY.MM.DD
  • <date formatting style> controls the date format in which the result is to be emitted. Recognized values for the second parameter are:
    • DMY gives d mmmm yyyy (e.g. 31 5月 2007). This is the default format.
    • MDY gives mmmm d, yyyy (e.g. 5月 31, 2007)
    • YMD gives yyyy mmmm d (e.g. 2007 5月 31)
    • ISO gives an ISO 8601 style yyyy-mm-dd (e.g. 2007-05-31)
    • none applies no formatting whatsoever
    If a date to be formatted is provided but no style is specified, the template emits a date in "d month yyyy" format, as it appears (for example) on discussion pages. When no date is specified, then the current date is emitted with no year, either in "d month" format for formatting style DMY, or in "month d" format for formatting styles MDY, YMD and ISO.
    Notes:
    • Although these are the four formats supported by MediaWiki's date autoformatting mechanism, only DMY/lDMY and MDY/lMDY are in accordance with the English language Wikipedia's Manual of Style. The use of ISO/lISO (ISO 8601) format within article prose is expressly discouraged by the Manual of Style; this format should only be used in tables, lists, or references.
    • In each case, the result is exactly the same as what would be emitted by MediaWiki's date autoformatting mechanism.
      However, dates are not manipulated by MediaWiki's date autoformatting mechanism even when linked. This permits dates to have a consistent appearance regardless of whether they are linked or not.
    • Wikipedia caches the current version of pages, and the template may display the date that the page was cached, rather than the current date.

Examples

With valid input

Format Input Output
not specified {{date|2006-08-04}} 4 8月 2006
{{date|Aug 4, 2006}} 4 8月 2006
{{date|4 August 2006}} 4 8月 2006
{{date|4 August}} 4 8月
{{date|Aug 4}} 4 8月
{{date|}} 22 11月 (missing year)
{{date}} 22 11月 2024
{{date|2=}} 22 11月 2024
DMY format {{date|2006-08-04|DMY}} 4 8月 2006
{{date|Aug 4, 2006|DMY}} 4 8月 2006
{{date|4 August 2006|DMY}} 4 8月 2006
{{date|4 August|DMY}} 4 8月
{{date|Aug 4|DMY}} 4 8月
{{date||DMY}} 22 11月 (missing year)
{{date|2=DMY}} 22 11月 2024
{{date|{{date}}|DMY}} 22 11月 2024
MDY format {{date|2006-08-04|MDY}} 8月 4, 2006
{{date|Aug 4, 2006|MDY}} 8月 4, 2006
{{date|4 August 2006|MDY}} 8月 4, 2006
{{date|4 August|MDY}} 8月 4
{{date|Aug 4|MDY}} 8月 4
{{date||MDY}} 11月 22 (missing year)
{{date|2=MDY}} 11月 22, 2024
{{date|{{date}}|MDY}} 22 11月 2024
YMD format {{date|2006-08-04|YMD}} 2006 8月 4
{{date|Aug 4, 2006|YMD}} 2006 8月 4
{{date|4 August 2006|YMD}} 2006 8月 4
{{date|4 August|YMD}} 8月 4
{{date|Aug 4|YMD}} 8月 4
{{date||YMD}} 11月 22 (missing year)
{{date|2=YMD}} 2024 11月 22
{{date|{{date}}|YMD}} 22 11月 2024
ISO format {{date|2006-08-04|ISO}} 2006-08-04
{{date|Aug 4, 2006|ISO}} 2006-08-04
{{date|4 August 2006|ISO}} 2006-08-04
{{date|4 August|ISO}} --08-04
{{date|Aug 4|ISO}} --08-04
{{date|{{#time: --m-d}}|ISO}} --11-22 (missing year)
{{date|2=ISO}} 2024-11-22
{{date|{{date}}|ISO}} 22 11月 2024
none format {{date|2006-08-04|none}} 2006-08-04
{{date|Aug 4, 2006|none}} Aug 4, 2006
{{date|4 August 2006|none}} 4 August 2006
{{date|4 August|none}} 4 August
{{date|Aug 4|none}} Aug 4
{{date||none}} (blank)
{{date|2=none}} (blank)
{{date|{{date}}|none}} 22 11月 2024

Other acceptable input

A wide variety of other input formats are handled correctly by the template:

Input Output
{{date|1st December 2008}} (ordinal 'st') 1 12月 2008
{{date|June 24 2006}} (missing comma) 24 6月 2006
{{date|24 june 2006}} (lowercase month name) 24 6月 2006
{{date|24 jun}} (abbreviated month name) 24 6月
{{date|June 2006}} 6月 2006
{{date|2006 June}} 1 6月 2006
{{date|05/31/2012}} 31 5月 2012
{{date|31-05-2012}} 31 5月 2012

Invalid input

Input strings that the template cannot format are returned unaltered. This includes strings containing characters such as square brackets, so any links will result in unformatted output.

Input Output
{{date|23007-031-24}} 23007-031-24
{{date|[[1 December]] [[2008]]|YMD}} 1 December 2008
{{date|1 December [[2008 in film|2008]]|MDY}} 1 December 2008
{{date|2008 December 1|DMY}} 2008 December 1 (expected: 1 12月 2008)
{{date|2008 December 1|MDY}} 2008 December 1 (expected: 12月 1, 2008)
{{date|2008 December 1|ISO}} 2008 December 1 (expected: 2008-12-01)
{{date|June 2006|ISO}} 6月 2006 (missing day, expected: 2006-06--)
{{date|--08-24}} --08-24
{{date|-08-24}} 22 11月 2024
{{date|08-24}} 08-24
{{date|2006-08--}} 2006-08--
{{date|2006-08-}} 2006-08-
{{date|32 Aug}} 32 Aug
{{date|932 Aug}} 932 Aug
{{date|0932 Aug}} 0932 Aug
{{date|Aug 32}} Aug 32
{{date|Aug 932}} Aug 932
{{date|Aug 0932}} Aug 0932
{{date|31/05/2012}} 31/05/2012 (expected 31 5月 2012)
{{date|05-31-2012}} 05-31-2012 (expected 31 5月 2012)
{{date|May 18, 2019|YDM}} 18 5月 2019 (expected 2019 18 5月)

Known issues

Out-of-range values are "fixed"
  • The PHP library, that this template ultimately depends on, converts out-of-range values to the next-best in-range values. As a result, some date-like values that are not actually valid dates get converted anyway. These include...
    • all dates with zero day-of-the-month. Such a "zero-th" day of a month gets converted into the last day of the previous month.
      For example, {{date|June 0, 2006}} results in 31 5月 2006.
      Similarly, {{date|0 January 2006}} becomes 31 12月 2005.
    • all dates with day-number values that exceed the number of days in the specified month cause the date to be incremented into the next month.
      For example, {{date|31 November 2008}} results in 1 12月 2008, since November has only 30 days.
      Similarly, {{date|February 29, 2007}} results in 1 3月 2007, since 2007 was not a leap year.
      Note: Due to a parser peculiarity in the PHP library, 'December 32' and '32 December' are treated differently, and the latter results in an error (which causes this template to emit it unchanged), and only the former "overflows" into the next month.
Non-Gregorian dates are treated as Gregorian
  • The PHP library that this template ultimately depends on assumes that all dates, irrespective of context, are Gregorian calendar dates. Consequently:
    • some dates that would be valid in a non-Gregorian context are treated as out-of-range dates and converted to the next-best in-range values. For example, {{date|30 February 1712}} results in 1 3月 1712 because – even though 30 February 1712 is a valid date in the Swedish calendar – February 1712 had only 29 days in the Gregorian calendar.
    • ten dates are not reformatted. These are 29 February & 1 March of the years 1000, 1100, 1300, 1400, 1500. The cause of this is the same as that of the Swedish 30 February 1712.
    • some dates that would not be valid in some contexts are converted normally. For example, 29 February 1912 or 1 February 1918 are not valid in a Russian context, but perfectly valid in the Gregorian one.
ISO 8601 format not suitable for years before 1583

The ISO 8601 standard requires mutual agreement between those using the format to use years earlier than 1583. Since no agreement has been obtained from our readers, that format should not be used for such years, nor for any date not in the Gregorian calendar.

Output provided for ISO dates when year is missing

The current 2004 version of ISO 8601 does not permit omitting the year, but this template will produce a supposedly ISO output when the year is missing.

Does not provide Microformats

See also

Multiple units of time

Type Adjustable Current Last Next
Date and time {{time}} {{Currentdate}} (MDY)
{{Plain now}} (DMY)
{{Now}} (DMY in a complete sentence)
{{Simple now}}
Date only {{Dateonly}}
{{Datedow}}
(Has the day of the week)
{{Date}} (Chose format)
{{DATE}} (prefixed by date=)
{{TODAY}} (DMY)
{{Yesterday}}
{{Day-1}}
{{Tomorrow}}
{{Day+1}}
Time only {{Timeonly}} {{CURRENTTIME}} (12 h format)
{{CURRENTTIME24}} (24 h format)
Banners {{Right now}}
(Blue with time and number of Wikipedia articles)
Timestamp {{Timestamp}}
(Format YYYYMMDDhhmmss)

Single units of time

Unit of time Adjustable Current Last Next
SECOND {{CURRENTSECOND}}
MINUTE {{CURRENTMINUTE}}
HOUR 模板:Mwl
WEEK 模板:Mwl
{{CURRENTWEEKOFMONTH}}
Day of Week {{DAYOFWEEK}} 模板:Mwl
{{CURRENTWEEKDAYABBREV}}
{{CURRENTWEEKDAY}}
MONTH {{MONTHNAME}}
{{MONTHABBREV}}
{{MONTHNUMBER}}
{{MONTH}} (zero padded)
模板:Mwl
模板:Mwl
模板:Mwl
{{CURRENTMONTHDAYS}}
{{LASTMONTHNAME}} {{NEXTMONTHNAME}}
Day of Month 模板:Mwl
模板:Mwl (zero padded)
YEAR {{CURRENTYEARCC}}
{{CURRENTYEARYY}}
{{CURRENTISOYEAR}}
模板:Mwl
{{LASTYEAR}} {{NEXTYEAR}}
Day of Year {{CURRENTDAYOFYEAR}}
DECADE {{DECADE}} {{CURRENTDECADE}}
CENTURY {{CENTURY}} {{CURRENTCENTURY}}
MILLENNIUM {{MILLENNIUM}}

Date and time templates made for substitution:

Month:
{{Lmonth}} produces 模板:Lmonth
{{Nmonth}} produces 模板:Nmonth
{{LastMonth}} produces 模板:LastMonth
{{NextMonth}} produces 模板:NextMonth


Individual templates

Categories of templates


Converts dates into a format used on Wikipedia

模板参数

参数描述类型状态
date1

Date to be formatted

示例值
Jan 1, 2007
日期推荐
format2

Controls the date format for the result

默认值
DMY
示例值
MDY
字符串推荐