how can i calculate age in years, months and days in a textfield from a datefield
I tried but failed
plz suggest
Solved! Go to Solution.
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
Views
Replies
Total Likes
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
Views
Replies
Total Likes
Thanks to help me
it give me result in a pop window
but i want result in Text field.
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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)
Views
Replies
Total Likes
I see what you mean, I've edited my answer above with the approach used by the momentJS library.
thank you so much BR001
You are great
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies