Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Difference between given two dates in days,months & years

Status
Not open for further replies.

rajarama

Programmer
Dec 15, 2000
12
0
0
IN
I am working on a program to calculate terminal benefits payable to epmloyees on their retirement. These benefits depend upon the service done by that employee. Service is to be countded in number of years , months and days.

Is there any function or procedure to caculate the same. So that after passing the two dates, diffence between them will be expressed as so many no of years + so many months + so many days.

Rajarama.
 
There is no function I know of to do this directly but you
could easily write one yourself along the lines of:-

Function TimeString(Start_Date, End_Date)
Private No_of_years, No_of_months, No_of_days, Total_Days
No_of_months = 0

Total_Days = End_Date - Start_Date

No_of_years = INT (Total_Days / 365)
No_of_days = MOD (Total_Days, 365)

** Split No_of_days into Months and Days - use Month()
** function to determine month no of start_date and work
** through the calendar year reducing no_of_days by that
** number which is in the month.

Start_Month_No = MONTH(Start_Date)

DO WHILE No_of_days > GETDAYS(Start_Month_No)

Days_till_next_month = GETDAYS(Start_Month_No)
IF Days_till_next_month < No_of_days
No_of_months = No_of_months + 1
No_of_days = No_of_days - Days_till_next_month
if Start_Month_No = 12
Start_Month_No = 1
else
Start_Month_No = Start_Month_No + 1
endif
EndIf
ENDDO

** We now have Numerical quantities for
** No_of_years, No_of_months and No_of Days
** Simply format this into a string and return value.

Return_String = alltrim(str(No_of_years) + &quot; years, &quot;+
alltrim(str(No_of_months) + &quot; months, &quot;+
alltrim(str(No_of_days) + &quot; days.&quot;

RETURN Return_String
*** End Function TimeString

FUNCTION GetDays(MonthNo)
PRIVATE Days_In_Month
*
DO CASE
CASE MonthNo = 1 &amp;&amp; January
Days_In_Month = 31
CASE Month_No = 2
Days_In_Month = 28
CASE
CASE ETC
CASE Month_No = 12
Days_In_Month = 31
ENDCASE
RETURN Days_In_Month
*** End Function GetDays

====================================================
Finally, you will need to make a 1 day adjustment to the
above functions for when the year passes by a leap year
and also when the no_of_days passes through February and it
is a leap year. I leave it to you to fill in this part.

Hope this helps.
AliFox.
 
M.SrvYears = year(endingdate) - year(StartingDate)
M.SrvMonths = abs(month(endingdate) - month(startingdate))
M.SrvDays = abs(day(endingdate) - day(startingdate))
David W. Grewe
Dave@internationalbid.com
ICQ VFP ActiveList #46145644
 
Rajarama

I have found the solution.Thanks to all those to helped and tried to find a solution.

The code is as below.
Get dates into variables as date1 and date2.
Confirm that date2 is greater than date1.

Stor 0 to y,m,d &amp;&amp; resultant no of years, months and days
do while gomo(date2 , -1 ) >= date1
m = m +1
date1 = gomo(date1 ,1 )
endd
y = int(m/12)
m = mod(m,12)
d = date2 - date1

? &quot;Years &quot; + str(y)
? &quot;months&quot; + str(m)
? &quot;days &quot; + str(d)
retu

Thanks you all.
Please let me know if there any bugs in this routine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top