locked
Looking for date field with leap year calculation. RRS feed

  • Question

  • I have a field A(Buying Date) and Field B(Expiring date) in Infopath form. When some one select's a buying date then Field B should update as +5 years from the buying date. How do I calculate the leap year here? I can't just add  formula //addDays(BuyingDate, 365(x)5) // . How will I know which year is leap year?

    Wednesday, March 30, 2011 5:39 PM

Answers

  • If you just need to make sure that you are adding 5 years (that is, using your addDays() method for today, 3/30/2011, yields 3/28/2016 because there are two leap year days and you really want 3/30/2016), you can do the following:

    1) Create a new text field, named CalcText for example,  on the form (this will eventually be hidden but for debug purposes you want to see it)

    2) Set the Default Value of CalcText to concat(string((substring-before(string(Date1), "-") + 5)), "-", substring-after(Date1, "-"))

    where Date1 is your field A (Buying Date) and 5 is the year value. Lucky for us the date picker value when converted to a string is in the format of YYYY-MM-DD.

    3) Set the Default Value of your Field B (Expiring Date) equal to the CalcText value.

    Initially, the CalcText will produce a NaN- because nothing is entered in the first date field. You can workaround that by hiding the second data field until something is selected in the first one. There may be a better way to keep it blank but I was more concerned on the date calculations.

    -=Steve

    • Marked as answer by Infopath_girl Thursday, March 31, 2011 5:50 AM
    Wednesday, March 30, 2011 6:13 PM

All replies

  • If you just need to make sure that you are adding 5 years (that is, using your addDays() method for today, 3/30/2011, yields 3/28/2016 because there are two leap year days and you really want 3/30/2016), you can do the following:

    1) Create a new text field, named CalcText for example,  on the form (this will eventually be hidden but for debug purposes you want to see it)

    2) Set the Default Value of CalcText to concat(string((substring-before(string(Date1), "-") + 5)), "-", substring-after(Date1, "-"))

    where Date1 is your field A (Buying Date) and 5 is the year value. Lucky for us the date picker value when converted to a string is in the format of YYYY-MM-DD.

    3) Set the Default Value of your Field B (Expiring Date) equal to the CalcText value.

    Initially, the CalcText will produce a NaN- because nothing is entered in the first date field. You can workaround that by hiding the second data field until something is selected in the first one. There may be a better way to keep it blank but I was more concerned on the date calculations.

    -=Steve

    • Marked as answer by Infopath_girl Thursday, March 31, 2011 5:50 AM
    Wednesday, March 30, 2011 6:13 PM
  • Thanks Steve. It's working :) I was trying some different combinations yesterday..didn't work.
    Thursday, March 31, 2011 5:49 AM