About This Code
Brief Description:
Comparing 2 dates and displaying the difference
Contributor:
David B Bohlin
Type:
Date/Time functions
Notes Version:
R4.x, R6.x, R5.x, R7.x
Last Modified:
02 Nov 2006
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
Just a simple little formula to compare two dates and display the results usefully. If the difference is 3 years or more then just the years are displayed. Less than a year only months, less than a month only days displayed.
Usage / Example
_days := @day(EarlyDate) - @day(LaterDate);
_months := @Month(EarlyDate) - @Month(LaterDate);
_years := @Year(EarlyDate) - @Year(LaterDate);
@if(_months < 0; @Do(_months := _months + 12; _years := _years -1);"");
@If(EarlyDate< LaterDate; "LaterDate is greater that EarlyDate";
_years > 2; @Text(_years) + " Years";
_years < 1 & _months > 0; @Text(_months) + " Months";
_months = 0; @Text(_days) + " Days";
@Text(_months) + " Mths " + @Text(_years) + " Yrs")
Comments
Posted by Luanne Cali on 04/14/2008 09:59:32 AMDates
This seems to be just what I need but I keep getting an error. I set up two field, EarlyDate and LaterDate they are Date/Time fields.
I set up a third field, text. Made it computed and put that formula in there. I get an error that the field is expected to be Time/Date because of the function. I change it to Date/Time. Same thing.
So then I thought it was because the fields the formula is base on are empty. I made a button to click on with the formula using an @SetField to put the text answer in. Never could get the right answer. I'm a newbie but I thought I understoon this formula. lcali@summitpolymers.com
Posted by Qaiser Abbas on 01/06/2009 12:29:19 AMCode for calculation of difference between two dates
I have tried above code but that code don't calculate date difference properly. Then following code has beend developed and uploaded. This code also cater leap year and produces actual difference in years, months and days.
Date1 :=@Date(FirstDate);
Date2:=@Date(LastDate);
Y1:=@Year(Date1);
M1:=@Month(Date1);
D1:=@Day(Date1);
Y2:=@Year(Date2);
M2:=@Month(Date2);
D2:=@Day(Date2);
@If(D1<=D2; MX:=M2; MX:=M2-1);
AdjD2 := @If(@Modulo(Y2;4)=0; @Select(MX;31; 29; 31;30;31;30;31;31;30;31;30;31); @Select(MX;31; 28; 31;30;31;30;31;31;30;31;30;31));
@If(D1<=D2; D3:=D2-D1; D3:=D2+AdjD2-D1);
@If(D1<=D2; ""; M2:=M2-1);
@If(M1<=M2; M3:=M2-M1; M3:=M2+12-M1);
@If(M1<=M2; ""; Y2:=Y2-1);
Y3:=Y2-Y1;
M4:=M2;
D4:=@If(@Modulo(Y2;4)=0; @Select(M4;31; 29; 31;30;31;30;31;31;30;31;30;31); @Select(M4;31; 28; 31;30;31;30;31;31;30;31;30;31));
@If(D3 + 1 = D4; M3:=M3+1;"");
@If(D3 + 1 = D4; D3:=0;D3:=D3+1);
@If(M3>=12; Y3:=Y3+1;"");
@If(M3>=12; M3:=0;"");
@Text(Y3) + " Year(s) " + @Text(M3) + " Month(s) " + @Text(D3) + " Day(s)"