Template:Century name from decade or year: Difference between revisions
Appearance
Content deleted Content added
fixes |
allow single-digit decades |
||
(16 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
<includeonly><!-- |
<includeonly><!-- |
||
# First check whether this is a year or a decade: YYYY or YYY0s |
# First check whether this is a year or a decade: YYYY or YYY0s |
||
# No regexes, so check separately for " |
# No regexes, so check separately for "YYYY" and "YYY0s" |
||
# In each case, the find function returns 0 for a nomatch, and 1 for a match, |
# In each case, the find function returns 0 for a nomatch, and 1 for a match, |
||
# so we add the two together and take a total of 0 as an error |
# so we add the two together and take a total of 0 as an error |
||
-->{{#ifeq: {{#expr: {{#invoke:String|find|{{{1|}}}|^%d%d%d%d$|plain=false}} + <!-- |
-->{{#ifeq: {{#expr: {{#invoke:String|find|{{{1|}}}|^%s*%d?%d?%d?%d%s*$|plain=false}} + <!-- |
||
-->{{#invoke:String|find|{{{1|}}}|^%d%d% |
-->{{#invoke:String|find|{{{1|}}}|^%s*%d?%d?%d?0s%s*$|plain=false}} }}<!-- |
||
-->|0<!-- |
-->|0<!-- |
||
-->|<!-- |
-->|<!-- |
||
# Doesn't match, so do nothing |
# Doesn't match, so do nothing unless the "nomatch" parameter is set |
||
⚫ | |||
⚫ | |||
⚫ | |||
-->|<!-- |
-->|<!-- |
||
# We have a match |
# We have a match |
||
# Years and decades need different calculations |
# Years and decades need different calculations |
||
-->{{#ifeq: {{#invoke:String|match|{{{1|}}}|^%d%d%d%ds$|1|1|false|%%NOMATCH%%}}|%%NOMATCH%%<!-- |
-->{{#ifeq: {{#invoke:String|match|{{{1|}}}|^%s*%d?%d?%d?%ds%s*$|1|1|false|%%NOMATCH%%}}|%%NOMATCH%%<!-- |
||
-->|<!-- |
-->|<!-- |
||
# It's a year |
# It's a year |
||
-->{{Ordinal|{{#expr: 1 + floor(({{{1|}}} |
-->{{Ordinal|{{#expr: 1 + floor(({{{1|}}} - 1) / 100)}}}}<!-- |
||
-->|<!-- |
-->|<!-- |
||
# It's a decade |
# It's a decade |
||
-->{{Ordinal|{{#expr: 1 + floor(({{#invoke:String|replace|{{{1|}}}|s$||1|false}} + 5) / 100)}}}}<!-- |
-->{{Ordinal|{{#expr: 1 + floor(({{#invoke:String|replace|{{{1|}}}|^%s*(%d?%d?%d?0)s%s*$|%1|1|false}} + 5) / 100)}}}}<!-- |
||
-->}}<!-- |
-->}}<!-- |
||
# Now use a dash if the "dash" parameter is set to yes, true, or 1 |
# Now use a dash if the "dash" parameter is set to yes, true, or 1 |
||
-->{{# |
-->{{#ifeq: {{{2|}}}|dash<!-- |
||
-->| |
-->|-<!-- Dash |
||
-->| |
-->| <!-- Space |
||
⚫ | |||
⚫ | |||
⚫ | |||
-->}}<!-- |
-->}}<!-- |
||
-->century<!-- |
-->century<!-- |
||
-->}}</includeonly>< |
-->}}</includeonly><noinclude> |
||
{{Documentation}}</noinclude> |
Latest revision as of 16:53, 8 May 2020
![]() | This template is used on approximately 112,000 pages. To avoid major disruption and server load, any changes should be tested in the template's /sandbox or /testcases subpages, or in your own user subpage. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
When supplied with a 1-, 2-, 3- or 4-digit year or decade, returns the name of the century.
Mostly used in categorisation.
Parameters
- The first parameter is required: either a 1-, 2-, 3-, or 4-digit year (e.g. 61, 905, 1839) or a 2-, 3-, or 4--digit decade (e.g. 60s, 900s, 1830s)
- The second parameter is optional. The single word "dash" changes the output from "nth century" to "nth-century"
- An optional "nomatch" parameter may be used to specify text to be return if the input cannot be processed. Default behaviour is return an empty string if there is no match
Usage
{{Century name from decade or year|YYYY}}
{{Century name from decade or year|YYYY|dash}}
{{Century name from decade or year|YYY0s}}
{{Century name from decade or year|YYY0s|dash}}
{{Century name from decade or year|YYYY|nomatch=FAIL}}
{{Century name from decade or year|YYYY|dash|nomatch=FAIL}}
{{Century name from decade or year|YYY0s|nomatch=FAIL}}
{{Century name from decade or year|YYY0s|dash|nomatch=FAIL}}
Examples
{{Century name from decade or year|8}}
→ 1st century{{Century name from decade or year|63}}
→ 1st century{{Century name from decade or year|421}}
→ 5th century{{Century name from decade or year|1707}}
→ 18th century{{Century name from decade or year|1514|dash}}
→ 16th-century{{Century name from decade or year|0s}}
→ 1st century{{Century name from decade or year|40s}}
→ 1st century{{Century name from decade or year|550s}}
→ 6th century{{Century name from decade or year|1920s}}
→ 20th century{{Century name from decade or year|2030s|dash}}
→ 21st-century
Errors
Empty or inappropriate parameters produce no output, unless the "nomatch" paremeter is set.
{{Century name from decade or year}}
→{{Century name from decade or year|7}}
→ 1st century{{Century name from decade or year|123abc}}
→{{Century name from decade or year|French flying flamingoes flounce furiously}}
→
With "nomatch"
{{Century name from decade or year|nomatch=FAILED}}
→ FAILED{{Century name from decade or year|7|nomatch=Aaaaaaaaaargh}}
→ 1st century{{Century name from decade or year|123abc|nomatch=garbage input}}
→ garbage input{{Century name from decade or year|Fastidious French flying flamingoes flounce furiously|nomatch=NOMATCH}}
→ NOMATCH
Redirects
TemplateData
This is the TemplateData for this template used by TemplateWizard, VisualEditor and other tools. See a monthly parameter usage report for Template:Century name from decade or year in articles based on its TemplateData.
TemplateData for Century name from decade or year
Returns the name of the century for a 1-, 2-, 3- or 4-digit year or decade.
Parameter | Description | Typ | Status | |
---|---|---|---|---|
Year or decade | 1 | a 1-, 2-, 3- or 4-digit-year (e.g. '1957') or a 4-digit-decade (e.g. '1760s')
| Number | optional |
dash | 2 | If this parameter is dash, then the century number is followed by a dash rather than a space, i.e. '16th-century' instead of the default '16th century'
| String | optional |
No match text | nomatch | If the input is not a 1-, 2-, 3- or 4-digit year or decade, output the "nomatch" value. Defaults to blank. | String | optional |
See also
- Template:Title year
- Template:Title year+1
- Template:Title year-1
- Template:Title year range
- Template:Title year2range
- Template:Title year2range+1
- Template:Title year2range-1
- Template:Title monthname
- Template:Title decade
- Template:Title century
- Template:Title number
- Template:Make YYYY–YY
- Template:Century name from title year
- Template:Century name from title decade
- Template:Century name from decade or year