Comparison of dates

Hi, everyone.

I have two dates in my form: Date of ART card reception and date of initiation of ART.

I want to make the following comparison: if date of initiation of ART is 6 days greater than Date of ART card reception…

How can I set this condition?

use the “date()” function

int(Date(#form/dateofartcard) - date(#form/dateofartinit))

this will return the number of days difference (might be negative number like this)

read up on this link it’s very useful. as well as a bunch of other stuff on that resource

https://confluence.dimagi.com/display/commcarepublic/Calculations+with+Dates+and+Times

Hi @Mazz,

How will I use the result of the date function (i.e. the number of days returned) for comparing the two dates?

Thanks

isn’t that what you wanted to do?

Hi @Mazz

Yes, That’s What I want to do.

1- Create a label type Question
2- type something like “Error entry - Date order is incorrect” or whatever error message you want to show
3- have a hidden calculation that counts the difference in days between the two dates like described above. call it, date_difference
4- in the validation field of the label type question from step 1, put “False()”
5- in the display condition of the same question from step 1, put something like “date_difference > 6”
6- save your form and release a new version of your app
7- enjoy

you can change the condition in the display conditions of the question in step 1 (label) and do whatever you want with that. if you get a negative number in the date_difference calculation, then it means the order is wrong. if you get a zero, then it’s the same date. you can have different messages that show up for different conditions. just add new label type questions and repeat those steps.
if the label doesn’t show up, the validation condition won’t apply on the form and it won’t cause an error preventing you from submitting the form. so you’ll be ok if the conditions are not met.

HTH

Mazz