Help:Time function

From Wikipedia, the free encyclopedia
Jump to navigationJump to search

The time function, or #time, is the MediaWiki parser function to display a date/time in other formats. For example:

  • {{#time:j F Y}}                shows: 25 June 2018, the current UTC date
  • {{#time:F j, Y}}               shows: June 25, 2018, the current date in mdy format
  • {{#time:Y-m-d}}                shows: 2018-06-25, current ISO 8601 date
  • {{#time:H:i:s}}                shows: 20:42:59, current time-of-day
  • {{#time:l}}                    shows  "Monday" as current day of week
  • {{#time:\d\a\y D}}             shows  "day Mon" as current 3-letter day
  • {{#time:F j. Y|7 May 2013|de}} shows  "Mai 7. 2013" as a date in German
  • {{#time:xij xiF xiY}}          shows  "4 Tir 1397" as current date in Iranian calendar

There are over 35 date format codes (see below: Time format codes) to rearrange the day-month-year, or hour-minute-second (such as 'j' for day, 'H' for hour, or el 'l' to show day-of-week). Other letters are treated as literal text, or use backslash to escape the format ('\d' shows 'd'). Parameter 2 can designate some other date/time (in any of several typical formats), while a 2-letter language code can be specified as parameter 3 to translate a month name into another language.

Beyond the Gregorian calendar, the #time function can also convert a date into some other calendars by using x-codes (x_): Islamic (xm), Iranian (xi), Hebrew (xj), Thai (xk), Minguo (xo), or the Japanese calendar (xt). For example, to show the Islamic date, as the month and year: {{#time:xmF xmY}} → Shawwal 1439.

The #time function can show numerals in some other number systems by using x-codes (x_), including: Hindi (xn or xN), Hebrew (xh), and Roman numerals (xr). For example, to show the current time, H:i:s, in Roman numerals: {{#time:xrH:xri:xrs}} → XX:XLII:LIX. See more examples below: Time format codes.

General features[edit]

The #time parser function takes a date and/or time (in the Gregorian calendar) and formats it according to the syntax given. A date/time object can be specified; the default is the value of the magic word {{CURRENTTIMESTAMP}} – that is, the time the page was last rendered into HTML.

{{#time: format string }}
{{#time: format string | date/time object }}
{{#time: format string | date/time object | language code }}

The list of accepted formatting codes is given in the table below. Any character in the formatting string that is not recognised is passed through unaltered; this applies also to blank spaces (the system does not need them for interpreting the codes). There are also two ways to escape characters within the formatting string:

  1. A backslash followed by a formatting character is interpreted as a single literal character
  2. Characters enclosed in double quotes are considered literal characters, and the quotes are removed.

In addition, the digraph xx is interpreted as a single literal "x".

{{#time: Y-m-d }}2018-06-25
{{#time: [[Y]] m d }}2018 06 25
{{#time: [[Y (year)]] }}2018 (18UTCpmMon, 25 Jun 2018 20:42:59 +0000)
{{#time: [[Y "(year)"]] }}2018 (year)
{{#time: i's" }}42'59"

The date/time object can be in any format accepted by PHP's strtotime() function. Both absolute (e.g., 20 December 2000) and relative (e.g., +20 hours) times are accepted.

{{#time: r|now}}Mon, 25 Jun 2018 20:42:59 +0000
{{#time: r|+2 hours}}Mon, 25 Jun 2018 22:42:59 +0000
{{#time: r|now + 2 hours}}Mon, 25 Jun 2018 22:42:59 +0000
{{#time: r|20 December 2000}}Wed, 20 Dec 2000 00:00:00 +0000
{{#time: r|December 20, 2000}}Wed, 20 Dec 2000 00:00:00 +0000
{{#time: r|2000-12-20}}Wed, 20 Dec 2000 00:00:00 +0000
{{#time: r|2000 December 20}}Error: Invalid time.

The two-letter language code in ISO 639-1 allows the string to be displayed in the chosen language.

{{#time:d F Y|1988-02-28|nl}}28 februari 1988
{{#time:d xg Y|20 June 2010|pl}}20 czerwca 2010

If you've calculated a Unix timestamp, you may use it in date calculations by pre-pending an @ symbol.

{{#time: U | now }}1529959379
{{#time: r|@1529959379}}Mon, 25 Jun 2018 20:42:59 +0000

Full or partial absolute dates can be specified; the function will "fill in" parts of the date that are not specified using the current values:

{{#time: Y | January 1 }}2018

Many options are provided.

Time format codes[edit]

The following table explains the 35 various codes for date/time formats, plus the codes for months in other languages, and the x-codes (x_) to convert to some other calendars or number systems.

CodeDescriptionCurrent output
(Purge this page's cache to update)
Y4-digit year.2018
y2-digit year.18
L1 if it's a leap year, 0 if not.0
o ¹ISO-8601 year of the specified week. ²2018 ³

¹ Requires PHP 5.1.0 and newer and rev:45208.
² This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead.
³ Will output literal o if ¹ not fulfilled.

nMonth index, not zero-padded.6
mMonth index, zero-padded.06
MAn abbreviation of the month name, in the site language.Jun
FThe full month name in the site language.June
xgOutput the full month name in the genitive form for site languages that distinguish between genitive and nominative forms.For Polish:


{{#time:d F Y|20 June 2010}|pl}} → 20 czerwiec 2010


{{#time:d xg Y|20 June 2010|pl}} → 20 czerwca 2010

WISO 8601 week number, zero-padded.26
jDay of the month, not zero-padded.25
dDay of the month, zero-padded.25
zDay of the year (January 1=0).
[[#ref_{{{1}}}|^]] To get the ISO day of the year add 1.
DAn abbreviation for the day of the week. Rarely internationalised.Mon
lThe full weekday name. Rarely internationalised.Monday
NISO 8601 day of the week (Monday=1, Sunday=7).1
wNumber of the day of the week (Sunday=0, Saturday=6).1
a"am" during the morning (00:00:00 → 11:59:59), "pm" otherwise (12:00:00 → 23:59:59).pm
AUppercase version of a above.PM
gHour in 12-hour format, not zero-padded.8
hHour in 12-hour format, zero-padded.08
GHour in 24-hour format, not zero-padded.20
HHour in 24-hour format, zero-padded.20
Minutes and seconds
iMinutes past the hour, zero-padded.42
sSeconds past the minute, zero-padded.59
USeconds since January 1 1970 00:00:00 GMT.1529959379
Timezone (as of 1.22wmf2)
eTimezone identifier.UTC
IWhether or not the date is in daylight savings time.0
ODifference to Greenwich time (GMT)+0000
PDifference to Greenwich time (GMT), with colon+00:00
TTimezone abbreviation.UTC
ZTimezone offset in seconds.0
tNumber of days in the current month.30
cISO 8601 formatted date, equivalent to Y-m-d"T"H:i:s+00:00.2018-06-25T20:42:59+00:00
rRFC 5322 formatted date, equivalent to D, j M Y H:i:s +0000, with weekday name and month name not internationalised.Mon, 25 Jun 2018 20:42:59 +0000
Non-Gregorian calendars
xmjDay of the month.11
xmFFull month name.Shawwal
xmnMonth index.10
xmYFull year.1439
Iranian (Jalaly)
xijDay of the month.4
xiFFull month name.Tir
xinMonth index.4
xiYFull year.1397
xiy2-digit year.97
xjjDay of the month.12
xjFFull month name.Tamuz
xjtNumber of days in month.29
xjxGenitive form of the month name.Tamuz
xjnMonth number.10
xjYFull year.5778
Thai solar
xkYFull year.2561
Minguo/Juche year
xoYFull year.107
Japanese nengo
xtYFull year.平成30
xnFormat the next numeric code as a raw ASCII number.In the Hindi language, {{#time:H, xnH}} produces ०६, 06.
xNLike xn, but as a toggled flag, which endures until the end of the string or until the next appearance of xN in the string.
xrFormat the next number as a Roman numeral. Only works for numbers up to 10,000
(up to 3,000 in pre MediaWiki 1.20).
{{#time:xrY}} → MMXVIII
xhFormat the next number as a Hebrew numeral.{{#time:xhY}} → ב'י"ח

See also[edit]