Expand my Community achievements bar.

Calculating Years and Months

Avatar

Former Community Member

I want to have a field that will calculate out the years and months a person has been employed at my business.  I know the calculations for getting the years is:

if (HasValue(dob)) then

          var currDate_ = Date2Num(currDate.formattedValue, "MM/DD/YYYY")

          var dob_ = Date2Num(dob.formattedValue, "MM/DD/YYYY")

          var diff = currDate_ - dob_

          $.rawValue = Floor(diff / 365.25)

else

          $.rawValue = null

endif

I want to  find it put out so it says 5 years 3 months if that is possible, or something close to that.

2 Replies

Avatar

Level 10

var intYears = Floor(diff / 365.25)

var intMonths = Floor((diff - (intYears * 365.25)) / 30.4375)

var strOutput = Concat(intYears, " years, ", intMonths, " months")

xfa.host.messageBox(strOutput);