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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number of months 2

Status
Not open for further replies.

JRudisill

Programmer
Joined
Aug 4, 2003
Messages
54
Location
US
I am probably just being stubborn and can't get ny head around this. I have two dates that i subtracting to get the number of days between. I then need to find out the number of months between the two and it has to be exact. The only thing I can come up with is to divide that value by 30.
 
Need to know what you're after. What's your definition of an exact number of months?

Jim
 
Are you talking acurate in a decimal sense (i.e. 1.8 months) ?

If so, the problem is that months are of different lengths, so where do you start?

However:

MS provides this solution for Excel - which rounds up or down in WHOLE months!

For a 'decimal' approach, I would probably look for a consistent approach, perhaps not an ideal one, of taking a month to be on average 365/12 days!

Does this help?

Regards

Griff
Keep [Smile]ing
 
I've just been reading up on this on various web sites and I THINK an appropriate method would be to calculate the number of whole months between the two dates (rounded down - so two days in the same month would be zero).

Then ADD the number of days from the end of the month for the earliest date and the number from the beginning of the month for the latest date over the sum of the number of days in each of those two months divided by two.

Unless the dates are both in the same month, in which case the number of months would be the number of days between the dates over the number of days in the month! Phew!

Does that make any sense?

I'm off to write a function...

Regards

Griff
Keep [Smile]ing
 
For a months and days approach:

FOR i = 1 TO 1000000
IF GOMONTH(begdate, i) > enddate
EXIT
ENDIF
ENDFOR

myMonths = i -1
myDays = enddate - GOMONTH(begdate, i - 1)

Jim
 
Hi,

This needs testing!

Code:
close all
clear
set date british
set century on
? NoMonths(ctod("01/01/2004"),ctod("21/01/2004"))
? NoMonths(ctod("01/01/2003"),ctod("21/01/2004"))



function NoMonths
	parameters eDate,lDate
	private eDate,lDate,myValue,tDate
	myValue = 0
	if lDate < eDate 
		tDate = eDate
		eDate = lDate
		lDate = tDate
	endif
	if lDate <> eDate
		 if Month(eDate) = Month(lDate) .and. Year(eDate) = Year(lDate)
		 	** calculate no days
	 		myValue = (lDate-eDate)/DaysInMonth(eDate)
	 	else
	 		myMonthsAvg = (DaysInMonth(eDate)+DaysInMonth(lDate))/2
	 		myValue = ((DaysInMonth(eDate)-Day(eDate))+Day(lDate))/myMonthsAvg
			myValue = myValue + IIF(DAY(LDate)>=DAY(EDate),0,-1)+(YEAR(LDate)-YEAR(EDate))*12+MONTH(LDate)-MONTH(EDate) - 1
		endif	
	endif
	
return(myValue)

Function DaysInMonth
	Parameter mDate
	Private mDate,mDays,mDateFmt
	mDateFmt = Set("Date")
	set Date to British
	mDays = Day(Ctod("1/"+alltrim(str(Month(mDate+32),2,0))+"/"+alltrim(str(Year(mDate+32),4,0)))-1)
	
	Set Date to (mDateFmt)
		
return(mDays)

Regards

Griff
Keep [Smile]ing
 
Here's another option:

Code:
*... months.prg
PARAMETERS dstartmonth, dendmonth

STORE dstartmonth TO dworkmonth
STORE 0 TO nCounter
DO WHILE dworkmonth <= dendmonth
   IF Gomonth(dworkmonth, 1) <= dendmonth
      nCounter = nCounter + 1 
   ELSE 
      ?'Months: '
      ?? nCounter
      ?'Days: '
      ?? dendmonth - dworkmonth
      EXIT 
   ENDIF
   dworkmonth = Gomonth(dworkmonth, 1)
ENDDO 
?dstartmonth, dendmonth, dworkmonth

RETURN nCounter

?months (Date(), {^2004/09/01})
?months (Date(), {^2004/09/30})
?months (Date(), {^2004/08/31} + 1)

-Dave Summers-
[cheers]
Even more Fox stuff at:
 
For sake of simplicity you could just do this:

lcFrontDate = CTOD("01/01/04") && Use beginning date here
lcBackDate = CTOD("07/01/04") && use ending date here
lnMonths = 0

DO WHILE lcFrontDate < lcBackDate
lnMonths = lnMonths + 1
lcFrontDate = GOMONTH(lcFrontDate,1)
ENDDO


 
I usually convert the whole date to months since time zero and then subract one from the other.

e.g.

(year(date1)*12+month(date1))-(year(date2)*12+month(date2))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top