Difference between revisions of "Age"

From Sega Retro

 
(3 intermediate revisions by one other user not shown)
Line 1: Line 1:
<includeonly>{{#if: {{{2|}}}
+
<includeonly>{{#vardefine:date2|{{#if:{{{2|}}}|{{{2|}}}|{{CURRENTYEAR}}-{{CURRENTMONTH}}-{{CURRENTDAY}}}}}}{{#vardefine:date1y|{{#sub:{{#explode:{{{1|}}}|-|0}}|0|4}}}}{{#vardefine:date2y|{{#sub:{{#explode:{{#var:date2}}|-|0}}|0|4}}}}{{#if:{{#pos:{{#var:date1y}}|x}}
| {{#vardefine:date2_y|{{year|{{{2}}}}}}}{{#vardefine:date2_m|{{month|{{{2}}}}}}}{{#vardefine:date2_d|{{#sub:{{#explode:{{{2}}}|-|2}}|0|2}}}}
+
| {{#vardefine:date1ymin|{{#replace:{{#var:date1y}}|x|0}}}}{{#vardefine:date1ymax|{{#replace:{{#var:date1y}}|x|9}}}}
| {{#vardefine:date2_y|{{CURRENTYEAR}}}}{{#vardefine:date2_m|{{CURRENTMONTH}}}}{{#vardefine:date2_d|{{CURRENTDAY}}}}
+
| {{#vardefine:date1ymin|{{#var:date1y}}}}{{#vardefine:date1ymax|{{#var:date1y}}}}
}}{{#vardefine:date1_y|{{year|{{{1}}}}}}}{{#vardefine:date1_m|{{month|{{{1}}}}}}}{{#vardefine:date1_d|{{#sub:{{#explode:{{{1}}}|-|2}}|0|2}}}}{{#if:{{#var:date1_d}}||{{#vardefine:date1_d|01}}}}<!-- Set day to 01 if undefined (for simplicity). -->{{#if:{{#var:date2_d}}||{{#vardefine:date2_d|01}}}}{{#vardefine:age|}}{{#ifexpr: {{#var:date2_m}} > {{#var:date1_m}}
+
}}{{#vardefine:date1m|{{#replace:{{#sub:{{#explode:{{{1|}}}|-|1}}|0|2}}|x}}}}{{#if:{{#var:date1m}}
| {{#vardefine:age|{{#expr:{{#var:date2_y}}-{{#var:date1_y}}}}}}
+
| {{#vardefine:date1mmin|{{#var:date1m}}}}{{#vardefine:date1mmax|{{#var:date1m}}}}
}}{{#ifexpr: {{#var:date2_m}} < {{#var:date1_m}}
+
| {{#vardefine:date1mmin|01}}{{#vardefine:date1mmax|12}}
| {{#vardefine:age|{{#expr:{{#var:date2_y}}-{{#var:date1_y}}-1}}}}
+
}}{{#vardefine:date1d|{{#replace:{{#sub:{{#explode:{{{1|}}}|-|2}}|0|2}}|x}}}}{{#if:{{#var:date1d}}
}}{{#ifexpr: {{#var:date2_m}} = {{#var:date1_m}}
+
| {{#vardefine:date1dmin|{{#var:date1d}}}}{{#vardefine:date1dmax|{{#var:date1d}}}}
| {{#ifexpr: {{#var:date2_d}} > {{#var:date1_d}}
+
| {{#vardefine:date1dmin|01}}{{#vardefine:date1dmax|31}}}}{{#if:{{#pos:{{#var:date2y}}|x}}
| {{#vardefine:age|{{#expr:{{#var:date2_y}}-{{#var:date1_y}}}}}}
+
| {{#vardefine:date2ymin|{{#replace:{{#var:date2y}}|x|0}}}}{{#vardefine:date2ymax|{{#replace:{{#var:date2y}}|x|9}}}}
}}{{#ifexpr: {{#var:date2_d}} < {{#var:date1_d}}
+
| {{#vardefine:date2ymin|{{#var:date2y}}}}{{#vardefine:date2ymax|{{#var:date2y}}}}
| {{#vardefine:age|{{#expr:{{#var:date2_y}}-{{#var:date1_y}}-1}}}}
+
}}{{#vardefine:date2m|{{#replace:{{#sub:{{#explode:{{#var:date2}}|-|1}}|0|2}}|x}}}}{{#if:{{#var:date2m}}
}}{{#ifexpr: {{#var:date2_d}} = {{#var:date1_d}}
+
| {{#vardefine:date2mmin|{{#var:date2m}}}}{{#vardefine:date2mmax|{{#var:date2m}}}}
| {{#vardefine:age|{{#expr:{{#var:date2_y}}-{{#var:date1_y}}}}}}
+
| {{#vardefine:date2mmin|01}}{{#vardefine:date2mmax|12}}
}}
+
}}{{#vardefine:date2d|{{#replace:{{#sub:{{#explode:{{#var:date2}}|-|2}}|0|2}}|x}}}}{{#if:{{#var:date2d}}
}}{{#ifeq: {{#var:date1_m}} | 00
+
| {{#vardefine:date2dmin|{{#var:date2d}}}}{{#vardefine:date2dmax|{{#var:date2d}}}}
| {{#vardefine:age|{{#expr:{{#var:date2_y}}-{{#var:date1_y}}-1}}-{{#expr:{{#var:date2_y}}-{{#var:date1_y}}}}}}
+
| {{#vardefine:date2dmin|01}}{{#vardefine:date2dmax|31}}
}}{{#var:age}}</includeonly><noinclude>
+
}}{{#vardefine:age1|{{age/CalcAge|{{#var:date1ymax}}-{{#var:date1mmax}}-{{#var:date1dmax}}|{{#var:date2ymin}}-{{#var:date2mmin}}-{{#var:date2dmin}}}}}}{{#vardefine:age2|{{age/CalcAge|{{#var:date1ymin}}-{{#var:date1mmin}}-{{#var:date1dmin}}|{{#var:date2ymax}}-{{#var:date2mmax}}-{{#var:date2dmax}}}}}}{{#ifeq:{{#var:age1}}|{{#var:age2}}
 +
| {{#var:age1}}
 +
| {{#if:{{{highonly|}}}||{{#var:age1}}}}{{#if:{{{lowonly|}}}{{{highonly|}}}||-}}{{#if:{{{lowonly|}}}||{{#var:age2}}}}
 +
}}</includeonly><noinclude>
 
Convert a date in the format YYYY-MM-DD to an age in years (based on the current date).
 
Convert a date in the format YYYY-MM-DD to an age in years (based on the current date).
  
 
==Usage==
 
==Usage==
<tt><nowiki>{{</nowiki>age|1991-06-23}}</tt>
+
* <nowiki>{{age|1991-06-23}}</nowiki> = {{age|1991-06-23}}
 
+
* <nowiki>{{age|1991}}</nowiki> = {{age|1991}}
returns
+
* <nowiki>{{age|1999-xx-xx}}</nowiki> = {{age|1999-xx-xx}}
 
+
* <nowiki>{{age|199x}}</nowiki> = {{age|199x}}
{{age|1991-06-23}}
+
* <nowiki>{{age|199x|2000}}</nowiki> = {{age|199x|2000}}
 +
* <nowiki>{{age|199x|lowonly=yes}}</nowiki> = {{age|199x|lowonly=yes}}
 +
* <nowiki>{{age|199x|highonly=yes}}</nowiki> = {{age|199x|highonly=yes}}
  
 
===Optional parameter===
 
===Optional parameter===
 
Add a second date to calculate the age on that date.
 
Add a second date to calculate the age on that date.
  
<tt><nowiki>{{</nowiki>age|1991-06-23|1999-09-09}}</tt>
+
* <nowiki>{{age|1991-06-23|1999-09-09}}</nowiki> = {{age|1991-06-23|1999-09-09}}
 
 
returns
 
 
 
{{age|1991-06-23|1999-09-09}}
 
  
[[Category:Sega Retro templates]]</noinclude>
+
[[Category:Templates]]</noinclude>

Latest revision as of 12:54, 1 January 2022

Convert a date in the format YYYY-MM-DD to an age in years (based on the current date).

Usage

  • {{age|1991-06-23}} = 33
  • {{age|1991}} = 32-33
  • {{age|1999-xx-xx}} = 24-25
  • {{age|199x}} = 24-34
  • {{age|199x|2000}} = 0-10
  • {{age|199x|lowonly=yes}} = 24
  • {{age|199x|highonly=yes}} = 34

Optional parameter

Add a second date to calculate the age on that date.

  • {{age|1991-06-23|1999-09-09}} = 8