Level 1

0% to

Level 2

Tip /

to gain points, level up, and earn exciting badges like the new
Mission!

View all

## Age in years, months and days

aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

0 solutions
View profile

aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

0 solutions
View profile
aka44356106
Level 2

23-11-2018

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

I tried but failed

plz suggest

#### Accepted Solutions (1)

_Bruce_Robertson
MVP

Likes

270 likes

Total Posts

1,782 posts

372 solutions
View profile

_Bruce_Robertson
MVP

Likes

270 likes

Total Posts

1,782 posts

372 solutions
View profile
_Bruce_Robertson
MVP

26-11-2018

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");

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

_Bruce_Robertson
MVP

Likes

270 likes

Total Posts

1,782 posts

372 solutions
View profile

_Bruce_Robertson
MVP

Likes

270 likes

Total Posts

1,782 posts

372 solutions
View profile
_Bruce_Robertson
MVP

27-11-2018

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

aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

0 solutions
View profile

aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

0 solutions
View profile
aka44356106
Level 2

28-11-2018

Dear BR001

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.

aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

0 solutions
View profile

aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

0 solutions
View profile
aka44356106
Level 2

28-11-2018

thank you so much BR001

You are great

aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

0 solutions
View profile

aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

0 solutions
View profile
aka44356106
Level 2

27-11-2018

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)

_Bruce_Robertson
MVP

Likes

270 likes

Total Posts

1,782 posts

372 solutions
View profile

_Bruce_Robertson
MVP

Likes

270 likes

Total Posts

1,782 posts

372 solutions
View profile
_Bruce_Robertson
MVP

27-11-2018

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

aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

0 solutions
View profile

aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

0 solutions