Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

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

Learn More

View all

Sign in to view all badges

Age in years, months and days

Avatar

Avatar
Validate 10
Level 2
aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

Correct Reply

0 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 10
View profile

Avatar
Validate 10
Level 2
aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

Correct Reply

0 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 10
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)

Accepted Solutions (1)

Avatar

Avatar
Boost 50
MVP
_Bruce_Robertson
MVP

Likes

270 likes

Total Posts

1,782 posts

Correct Reply

372 solutions
Top badges earned
Boost 50
Boost 5
Boost 3
Boost 250
Boost 25
View profile

Avatar
Boost 50
MVP
_Bruce_Robertson
MVP

Likes

270 likes

Total Posts

1,782 posts

Correct Reply

372 solutions
Top badges earned
Boost 50
Boost 5
Boost 3
Boost 250
Boost 25
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");

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

Answers (6)

Answers (6)

Avatar

Avatar
Boost 50
MVP
_Bruce_Robertson
MVP

Likes

270 likes

Total Posts

1,782 posts

Correct Reply

372 solutions
Top badges earned
Boost 50
Boost 5
Boost 3
Boost 250
Boost 25
View profile

Avatar
Boost 50
MVP
_Bruce_Robertson
MVP

Likes

270 likes

Total Posts

1,782 posts

Correct Reply

372 solutions
Top badges earned
Boost 50
Boost 5
Boost 3
Boost 250
Boost 25
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.

Avatar

Avatar
Validate 10
Level 2
aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

Correct Reply

0 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 10
View profile

Avatar
Validate 10
Level 2
aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

Correct Reply

0 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 10
View profile
aka44356106
Level 2

28-11-2018

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.

Avatar

Avatar
Validate 10
Level 2
aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

Correct Reply

0 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 10
View profile

Avatar
Validate 10
Level 2
aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

Correct Reply

0 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 10
View profile
aka44356106
Level 2

28-11-2018

thank you so much BR001

You are great

Avatar

Avatar
Validate 10
Level 2
aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

Correct Reply

0 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 10
View profile

Avatar
Validate 10
Level 2
aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

Correct Reply

0 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 10
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)

Avatar

Avatar
Boost 50
MVP
_Bruce_Robertson
MVP

Likes

270 likes

Total Posts

1,782 posts

Correct Reply

372 solutions
Top badges earned
Boost 50
Boost 5
Boost 3
Boost 250
Boost 25
View profile

Avatar
Boost 50
MVP
_Bruce_Robertson
MVP

Likes

270 likes

Total Posts

1,782 posts

Correct Reply

372 solutions
Top badges earned
Boost 50
Boost 5
Boost 3
Boost 250
Boost 25
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

Avatar

Avatar
Validate 10
Level 2
aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

Correct Reply

0 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 10
View profile

Avatar
Validate 10
Level 2
aka44356106
Level 2

Likes

10 likes

Total Posts

56 posts

Correct Reply

0 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 10
View profile
aka44356106
Level 2

27-11-2018

Thanks to help me

it give me result in a pop window

but i want result in Text field.