Difference between revisions of "Age"

From Sega Retro

Line 1: Line 1:
 
<includeonly>{{#if: {{{2|}}}
 
<includeonly>{{#if: {{{2|}}}
| {{#vardefine:date2_y|{{year|{{{2}}}}}}}{{#vardefine:date2_m|{{month|{{{2}}}}}}}{{#vardefine:date2_d|{{#explode:{{{2}}}|-|2}}}}
+
| {{#vardefine:date2_y|{{year|{{{2}}}}}}}{{#vardefine:date2_m|{{month|{{{2}}}}}}}{{#vardefine:date2_d|{{#sub:{{#explode:{{{2}}}|-|2}}|0|2}}}}
 
| {{#vardefine:date2_y|{{CURRENTYEAR}}}}{{#vardefine:date2_m|{{CURRENTMONTH}}}}{{#vardefine:date2_d|{{CURRENTDAY}}}}
 
| {{#vardefine:date2_y|{{CURRENTYEAR}}}}{{#vardefine:date2_m|{{CURRENTMONTH}}}}{{#vardefine:date2_d|{{CURRENTDAY}}}}
}}{{#vardefine:date1_y|{{year|{{{1}}}}}}}{{#vardefine:date1_m|{{month|{{{1}}}}}}}{{#vardefine:date1_d|{{#explode:{{{1}}}|-|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: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:age|{{#expr:{{#var:date2_y}}-{{#var:date1_y}}}}}}
 
| {{#vardefine:age|{{#expr:{{#var:date2_y}}-{{#var:date1_y}}}}}}
 
}}{{#ifexpr: {{#var:date2_m}} < {{#var:date1_m}}
 
}}{{#ifexpr: {{#var:date2_m}} < {{#var:date1_m}}

Revision as of 14:02, 11 August 2017

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

Usage

{{age|1991-06-23}}

returns

33

Optional parameter

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

{{age|1991-06-23|1999-09-09}}

returns

8