1. Design
an InfoPath form template as shown in figure 1 with two Date Picker
controls named
startDate and endDate, and one Text Box
control named difference.
Figure
1. InfoPath form template in Design mode.
The Main data source of the InfoPath
form template should resemble the following figure:
Figure
2. The Main data source of the InfoPath form template.
2. Add the following Rule to
the startDate field:
Action: Set a field's value
Field: difference
Value:
(number(substring(../my:endDate, 9, 2)) + floor((153 *
(number(substring(../my:endDate, 6, 2)) + 12 * (floor((14 - number(substring(../my:endDate,
6, 2))) div 12)) - 3) + 2) div 5) + (number(substring(../my:endDate, 1, 4)) +
4800 - (floor((14 - number(substring(../my:endDate, 6, 2))) div 12))) * 365 +
floor((number(substring(../my:endDate, 1, 4)) + 4800 - (floor((14 - number(substring(../my:endDate,
6, 2))) div 12))) div 4) - floor((number(substring(../my:endDate, 1, 4)) + 4800
- (floor((14 - number(substring(../my:endDate, 6, 2))) div 12))) div 100) +
floor((number(substring(../my:endDate, 1, 4)) + 4800 - (floor((14 - number(substring(../my:endDate,
6, 2))) div 12))) div 400) - 32045) - (number(substring(., 9, 2)) + floor((153
* (number(substring(., 6, 2)) + 12 * (floor((14 - number(substring(., 6, 2)))
div 12)) - 3) + 2) div 5) + (number(substring(., 1, 4)) + 4800 - (floor((14 -
number(substring(., 6, 2))) div 12))) * 365 + floor((number(substring(., 1, 4))
+ 4800 - (floor((14 - number(substring(., 6, 2))) div 12))) div 4) -
floor((number(substring(., 1, 4)) + 4800 - (floor((14 - number(substring(., 6,
2))) div 12))) div 100) + floor((number(substring(., 1, 4)) + 4800 - (floor((14
- number(substring(., 6, 2))) div 12))) div 400) - 32045)
with the following Conditions
on the Rule:
startDate is not blank and
endDate is not blank
3. Add a second Rule to the startDate
field with the following settings:
Action: Set a field's value
Field: difference
Value: 0
with the following Conditions
on the Rule:
startDate is blank or
endDate is blank
4. Add the following Rule to
the endDate field:
Action: Set a field's value
Field: difference
Value:
(number(substring(.,
9, 2)) + floor((153 * (number(substring(., 6, 2)) + 12 * (floor((14 -
number(substring(., 6, 2))) div 12)) - 3) + 2) div 5) + (number(substring(., 1,
4)) + 4800 - (floor((14 - number(substring(., 6, 2))) div 12))) * 365 +
floor((number(substring(., 1, 4)) + 4800 - (floor((14 - number(substring(., 6,
2))) div 12))) div 4) - floor((number(substring(., 1, 4)) + 4800 - (floor((14 -
number(substring(., 6, 2))) div 12))) div 100) + floor((number(substring(., 1,
4)) + 4800 - (floor((14 - number(substring(., 6, 2))) div 12))) div 400) -
32045) - (number(substring(../my:startDate, 9, 2)) + floor((153 *
(number(substring(../my:startDate, 6, 2)) + 12 * (floor((14 -
number(substring(../my:startDate, 6, 2))) div 12)) - 3) + 2) div 5) +
(number(substring(../my:startDate, 1, 4)) + 4800 - (floor((14 -
number(substring(../my:startDate, 6, 2))) div 12))) * 365 +
floor((number(substring(../my:startDate, 1, 4)) + 4800 - (floor((14 -
number(substring(../my:startDate, 6, 2))) div 12))) div 4) - floor((number(substring(../my:startDate,
1, 4)) + 4800 - (floor((14 - number(substring(../my:startDate, 6, 2))) div
12))) div 100) + floor((number(substring(../my:startDate, 1, 4)) + 4800 -
(floor((14 - number(substring(../my:startDate, 6, 2))) div 12))) div 400) - 32045)
with the following Conditions
on the Rule:
startDate is not blank and
endDate is not blank
5. Add a second Rule to the endDate
field with the following settings:
Action: Set a field's value
Field: difference
Value: 0
with the following Conditions
on the Rule:
startDate is blank or
endDate is blank
6. Add the following Rule to
the difference field:
Action: Set a field's value
Field: .
Value: 0
with the following Condition
on the Rule:
difference does not match pattern
Custom Pattern: -{0,1}\d+
You should now have a fully functional InfoPath form that
will calculate the difference between the dates soon after you have entered
valid dates. This solution also works for InfoPath 2003 form templates and
InfoPath 2007 browser-enabled form templates.
Nice
ReplyDeleteExcellent !!!
ReplyDeleteas I can do without weekends and holidays?