I am currently working on a form in InfoPath where users enter a specific date in a field.
I then want a formula to calculate how many days that remain of the year. I also want to value each day to a cost. Say each day cost 2$ and the entered date is December 29th. I then want the function to return “price: 4$”
XPath 2.0 has an awful lot of date/time functions:
To get the days between the last day of 2012 and the current day, you can use:
If you have an user inputed date
$user-dateyou need to replacecurrent-date()with that$user-dateand to construct a date object for the last december day in that year, which you can do like:(or use
date($user-date), if$user-dateis a string)And to get the final price string use:
XPath 1.0:
Without date function you need to do all these stupid calculation by hand.
You need a map mapping the days from each month to the end of the year. Then you can do
Map[$month] - $day. (Or use a map mapping the months to the days to the beginning of the year and subtract that from 365, what I do, since I have that map already lying around)Now XPath 1.0 does not have maps, but you can simulate one with a string:
This works for every non leap year.
For a leap year, you just use 366 and add 1 to each month after february:
To detect the leap year you could do some modulo calculation, it is probably easier to just use a map of all leap years:
this returns 1 if the year is a leap year in the range
Then put it all together: