Auto-calculating date fields

Hi all,

I may be asking this question in the wrong place as it maybe more a
question about Xforms/Xpath, but thought I would give it a go...

I'm looking to automatically prefill one of the date questions with a
date based on a calculation from a previous question (ignoring the
Ethiopian date format issue for now!), so it the default answer would
be a few months from whichever date is entered on the previous
question - but we need to allow the flexibility for the user to edit
this if needed (so we can't just calculate from the database end).

There seem to be limited date/time functions in Xpath, but I had a go
with the following: https://hew-datacollect.appspot.com/www/formXml?readable=true&formId=dateaddtest_v1.0

  • though pretty sure this isn't right usage of the months functions.
    Anyway I have 2 questions:
  1. The above form uploads to Aggregate with no errors, but when I try
    to run on ODKCollect I get the message "XPath evaluation: cannot
    handle function 'months'". Is there a way to enable support for the
    date/time functions in ODKCollect?

  2. Given the date/time functions available in Xpath, does anyone know
    how to get it to just add months to a given date. I've seen lots of
    example about calculating the number of days until Christmas etc, but
    nothing showing adding durations to dates - perhaps it;s not possible?

Thanks in advance for any help - or pointers if there are more
appropriate forums for these questions or if there are alternative
suggestions as to how I could achieve this.

Cheers,

Alex

alex,

i can't see your form, but from what you describe, i don't think
javarosa core supports "months". the reason aggregate doesn't blow up
is because it just cares about the data storage.

what we've done in the past given these limitations (usually to figure
out ages) is to approximate the number of months and use that number.
the bind will look something like this.

<xf:bind nodeset="/data/ApproximateAge" type="int"
calculate="if(/data/AgeType='BirthDate', ((now()-/data/BirthDate) div
30.4375), (if(/data/AgeInMonths > 0, ((/data/AgeInYears *
12)+/data/AgeInMonths), (/data/AgeInYears * 12))))"/>

you can see a more complete version of the form at http://dpaste.com/602640.

the alternative is to add the months functionality to the
javarosa-core. maybe dimagi could comment on how hard/easy this would
be?

yaw

··· On Thu, Aug 25, 2011 at 11:58, Alex Little wrote: > Hi all, > > I may be asking this question in the wrong place as it maybe more a > question about Xforms/Xpath, but thought I would give it a go... > > I'm looking to automatically prefill one of the date questions with a > date based on a calculation from a previous question (ignoring the > Ethiopian date format issue for now!), so it the default answer would > be a few months from whichever date is entered on the previous > question - but we need to allow the flexibility for the user to edit > this if needed (so we can't just calculate from the database end). > > There seem to be limited date/time functions in Xpath, but I had a go > with the following: https://hew-datacollect.appspot.com/www/formXml?readable=true&formId=dateaddtest_v1.0 > - though pretty sure this isn't right usage of the months functions. > Anyway I have 2 questions: > > 1) The above form uploads to Aggregate with no errors, but when I try > to run on ODKCollect I get the message "XPath evaluation: cannot > handle function 'months'". Is there a way to enable support for the > date/time functions in ODKCollect? > > 2) Given the date/time functions available in Xpath, does anyone know > how to get it to just add months to a given date. I've seen lots of > example about calculating the number of days until Christmas etc, but > nothing showing adding durations to dates - perhaps it;s not possible? > > Thanks in advance for any help - or pointers if there are more > appropriate forums for these questions or if there are alternative > suggestions as to how I could achieve this. > > Cheers, > > Alex > > -- > Post: opendatakit@googlegroups.com > Unsubscribe: opendatakit+unsubscribe@googlegroups.com > Options: http://groups.google.com/group/opendatakit?hl=en >

Thanks Yaw. That makes sense.

Will see if I can work out how to add the months using the available
functions, we don't need a totally accurate date as it's an expected
baby delivery date, so within a few days is fine.

Cheers,
Alex

··· On Aug 25, 8:42 pm, Yaw Anokwa wrote: > alex, > > i can't see your form, but from what you describe, i don't think > javarosa core supports "months". the reason aggregate doesn't blow up > is because it just cares about the data storage. > > what we've done in the past given these limitations (usually to figure > out ages) is to approximate the number of months and use that number. > the bind will look something like this. > > calculate="if(/data/AgeType='BirthDate', ((now()-/data/BirthDate) div > 30.4375), (if(/data/AgeInMonths > 0, ((/data/AgeInYears * > 12)+/data/AgeInMonths), (/data/AgeInYears * 12))))"/> > > you can see a more complete version of the form athttp://dpaste.com/602640. > > the alternative is to add the months functionality to the > javarosa-core. maybe dimagi could comment on how hard/easy this would > be? > > yaw > > > > On Thu, Aug 25, 2011 at 11:58, Alex Little wrote: > > Hi all, > > > I may be asking this question in the wrong place as it maybe more a > > question about Xforms/Xpath, but thought I would give it a go... > > > I'm looking to automatically prefill one of the date questions with a > > date based on a calculation from a previous question (ignoring the > > Ethiopian date format issue for now!), so it the default answer would > > be a few months from whichever date is entered on the previous > > question - but we need to allow the flexibility for the user to edit > > this if needed (so we can't just calculate from the database end). > > > There seem to be limited date/time functions in Xpath, but I had a go > > with the following:https://hew-datacollect.appspot.com/www/formXml?readable=true&formId=... > > - though pretty sure this isn't right usage of the months functions. > > Anyway I have 2 questions: > > > 1) The above form uploads to Aggregate with no errors, but when I try > > to run on ODKCollect I get the message "XPath evaluation: cannot > > handle function 'months'". Is there a way to enable support for the > > date/time functions in ODKCollect? > > > 2) Given the date/time functions available in Xpath, does anyone know > > how to get it to just add months to a given date. I've seen lots of > > example about calculating the number of days until Christmas etc, but > > nothing showing adding durations to dates - perhaps it;s not possible? > > > Thanks in advance for any help - or pointers if there are more > > appropriate forums for these questions or if there are alternative > > suggestions as to how I could achieve this. > > > Cheers, > > > Alex > > > -- > > Post: opendatakit@googlegroups.com > > Unsubscribe: opendatakit+unsubscribe@googlegroups.com > > Options:http://groups.google.com/group/opendatakit?hl=en