Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Using Tap Forms › Calculating age from year
- This topic has 3 replies, 2 voices, and was last updated 4 years, 1 month ago by Sam Moffatt.
-
AuthorPosts
-
October 6, 2020 at 3:56 PM #42159
Stefan de GraafParticipantGood day! I’ve been experimenting with Tap Forms and really like the flexibility so far. Working on my first forms, but I can’t seem to figure this out:
I currently have a number-field with a year in it (called “Year”). Then I added a calculation-field (called “Age”) which should use that field to determine the age of the item. What I tried:
YEARS(NEWDATE(Year; 1; 0; 0; 0; 1; 0; 0; 0); TODAY())
But the age-field stays empty (also tried all of the result types). I also tried converting the year-field to an actual date (although I don’t really need a date for this) and trying this, but also no result:
YEARS(Year;TODAY());
Anyone have an idea what I’m doing wrong? Also it would be great if I could get an example for an actual date-field and for a number-field as I’ll need both in my forms, but can’t seem to get either to work. I’ve also added screenshots of both tries and the field settings
Thanks in advance for the help!
Best regards,
StefanAttachments:
You must be logged in to view attached files.October 7, 2020 at 1:06 AM #42171
Sam MoffattParticipantIn Try 1 you have Year as a number (good) and Age as a date (incorrect). You need the calculation field to be a number because the ages is going to be a number. That said date should have still shown up but still been wrong. I copied what you put on the forum, replace “Year” with the field placeholder for my form and it works when set to number.
In Try 2 you have an extra colon at the end of your calculation field. I created a date field and calc field also set to number which seemed to work properly for my tests.
Calculation fields are a little touchy at times and can give you an empty result without any feedback. I generally prefer script fields because it’s a little more obvious what the error is, that said it’s not all that friendly:
var year_date_field = record.getFieldValue('fld-060389f539934aa9b7163c7a8b7a4cd1'); parseInt((new Date() - year_date_field) / 1000 / 60 / 60 / 24 / 365.25)
Essentially we take the current date from the year_date_field (it’s a date field, a number field would need to be converted from a year to a date like the calc field does) and then that gives us the difference in milliseconds. We need to convert that to years which is the division operations there. The
parseInt
piece is to convert it to a whole number, alternatively you could replaceparseInt
withMath.floor
as well.Hopefully that helps!
October 7, 2020 at 5:45 AM #42190
Stefan de GraafParticipantThank you very much Sam! Both suggestions seem to work now (figured out that the first one worked all along but apparantly it doesn’t always recalculate the value when you adjust the calculation. when I changed the Year field, it did show up.. oops..).
And I didn’t even notice the script field (thanks for that suggestion!), otherwise I would’ve tried that as well. Might also be a nice way to generate text like “1 year, 10 months, 3 days ago”. Looks like it uses (some variant) of javascript, so that should be doable. Will try that tonight!
Thanks again for the help and quick reply!
October 7, 2020 at 5:44 PM #42203
Sam MoffattParticipantGood to hear you got it sorted, recalculation is a little magical but should happen when the field changes. If you suspect something funky, you can always hit the refresh button below the record that will trigger a recalc of all calc fields in the record (script fields too). Sometimes useful for flushing out the gremlins.
The script field is a front end to Apple’s JavaScript Core which is itself an implementation of ECMAScript 2015 (or later?). Script fields behave like calculation fields but execute Javascript and there is also a form script which can be executed on demand.
Apple’s JavaScript Core doesn’t have stuff outside the ECMAScript standard that you’d expect to see commonly from web browsers but the core standard JavaScript is accessible. To interact with Tap Forms is a specific JavaScript API that gives you access to some of the internals of Tap Forms. I have some examples on my GitHub of JavaScript and there is also the Script Talk forum focused on JavaScript. If you’re a beginner, T. L. Ford’s got a Tap Forms Javascript Scripting 101 tutorial too.
-
AuthorPosts
You must be logged in to reply to this topic.