Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Age in years, months and days

Avatar

Level 4

how can i calculate age in years, months and days in a textfield from a datefield

I tried but failed

plz suggest

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi,

There's nothing built-in to do this, but you could try this JavaScript

const _MS_PER_DAY = 1000 * 60 * 60 * 24;

const _DAYS_TO_MONTH = 4800 / 146097; // 400 years have 146097 days and 400 years have 4800 months

const _MONTH_TO_DAYS = 146097 / 4800;

var date = util.scand("yyyy-mm-dd", DateOfBirth.rawValue);

var now = new Date();

var days = Math.ceil((now - date) / _MS_PER_DAY);

var monthsFromDays = Math.floor(days * _DAYS_TO_MONTH);

var months = monthsFromDays;

days -= Math.ceil(monthsFromDays * _MONTH_TO_DAYS);

var years = Math.floor(months / 12);

months %= 12;

var result = [];

if (years > 1) result.push(years + " years");

if (years == 1) result.push(years + " year");

if (months > 1) result.push(months + " months");

if (months == 1) result.push(months + " month");

if (days > 1) result.push(days + " days");

if (days == 1) result.push(days + " day");

app.alert(result.join(", "));

My date field is called DateOfBirth and if you want to store the result in a text field you will need to set its .rawValue property instead of the app.alert()

This also assume they haven't entered a date in the future.

Regards

Bruce

View solution in original post

7 Replies

Avatar

Correct answer by
Level 10

Hi,

There's nothing built-in to do this, but you could try this JavaScript

const _MS_PER_DAY = 1000 * 60 * 60 * 24;

const _DAYS_TO_MONTH = 4800 / 146097; // 400 years have 146097 days and 400 years have 4800 months

const _MONTH_TO_DAYS = 146097 / 4800;

var date = util.scand("yyyy-mm-dd", DateOfBirth.rawValue);

var now = new Date();

var days = Math.ceil((now - date) / _MS_PER_DAY);

var monthsFromDays = Math.floor(days * _DAYS_TO_MONTH);

var months = monthsFromDays;

days -= Math.ceil(monthsFromDays * _MONTH_TO_DAYS);

var years = Math.floor(months / 12);

months %= 12;

var result = [];

if (years > 1) result.push(years + " years");

if (years == 1) result.push(years + " year");

if (months > 1) result.push(months + " months");

if (months == 1) result.push(months + " month");

if (days > 1) result.push(days + " days");

if (days == 1) result.push(days + " day");

app.alert(result.join(", "));

My date field is called DateOfBirth and if you want to store the result in a text field you will need to set its .rawValue property instead of the app.alert()

This also assume they haven't entered a date in the future.

Regards

Bruce

Avatar

Level 4

Thanks to help me

it give me result in a pop window

but i want result in Text field.

Avatar

Level 10

If you have a text field object then it would be something like;

TextField1.rawValue = result.join(", ");

If you have a text object then it would be

Text1.value.text.value = result.join(", ");

If you have a text object with rich text content then it would be something like;

var body = <body xmlns="http://www.w3.org/1999/xhtml"
xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">{result.join(", ")}</body>;

Text1.value.exData.loadXML(body.toXMLString(), /* Ignore root node */ false, /* Overwrite content */ true);

Again you will have to change TextField1 or Text1 to suit the names used in your form

Avatar

Level 4

its work like charm

but it don't show correct calculation

when today day < dob day,  today month<dob month, today year< dob year

eg. DOB = 29 oct 2018 and today day = 28/11/2018   Result is 1 month

and same result 1 month  for next date of Oct2018 (30Oct2018,31Oct2018

DOB = 29 dec 2017 and today date = 28/11/2018   Result => 1 year

and same result 1 year  for next date of Dec2017 (30dec2017, 31dec2017)

Avatar

Level 10

I see what you mean, I've edited my answer above with the approach used by the momentJS library.

Avatar

Level 4

Dear BR001

                        Please help me in a another case

i applied this script in a cell of a table that grows using the Button object

but when new row add then in new cell same old result show.