I need to be able to get the difference between now and a past date in year months days. So I have a method that accepts a date and returns a string with the result.
Its seems to work but I feel there should be a more elegant way.
I also realised I should comment my code, …
datediff(sd as date )as string dim now as new date //date,time now dim rs as string //return string dim t,ddays,dmonths as integer dim mth() as integer mth = array(31,28,31,30,31,30,31,31,30,31,30,31) //days in each month t=(now.TotalSeconds-sd.TotalSeconds) //number of second between now and the selected date t = t/(365.24*24*60*60) //seconds divided by number seconds in a year to give years if t>0 then rs =str(t)+" Year" if t >1 then rs=rs+"s" end if end if // now get the days t=0 if sd.day <> now.day then ddays=now.day-sd.day if ddays<0 then ddays=ddays+mth(((now.Month+11)mod 12)+1) // If t=1 //carry to use for month calc if we have borrowed due to the sd.day being larger than the now.day end if end if // now the months if sd.month<>now.month or t=1 then dmonths=now.month-sd.month-t if dmonths<0 then dmonths=dmonths+12 end if rs=rs+" "+str(dmonths)+" Month" if dmonths>1 then rs=rs+"s" // if more than 1 month then add an s to make months end if end if rs=rs+" "+str(ddays)+" day" if t>0 then rs=rs+"s" //if more than 1 day make pleural ie days end if return rs