Way to figure out number of months difference between two dates 
Author Message
 Way to figure out number of months difference between two dates

Is there a date difference function offered by FoxPro 5.0 that will
return to me the number of months difference between 2 dates. My dates
are in either format MM/DD/YY or MM/DD/YYYY (they are defined as date
fields in my database).

Do I have to write my own? If I do if anyone has a function that
handles this already that would be great too. I am sure I can figure
it out on my own too.
--
Laura Erskine. To reply remove STTYOPALL_ from email address.
____



Sat, 12 Jan 2002 03:00:00 GMT  
 Way to figure out number of months difference between two dates
Laura,

I don't believe there is a native FP command that will do this for you, but
you should be able to write your own routine using a combination of date
subtraction and the GoMonth() function.

Dan


Quote:
> Is there a date difference function offered by FoxPro 5.0 that will
> return to me the number of months difference between 2 dates. My dates
> are in either format MM/DD/YY or MM/DD/YYYY (they are defined as date
> fields in my database).

> Do I have to write my own? If I do if anyone has a function that
> handles this already that would be great too. I am sure I can figure
> it out on my own too.
> --
> Laura Erskine. To reply remove STTYOPALL_ from email address.
> ____



Sat, 12 Jan 2002 03:00:00 GMT  
 Way to figure out number of months difference between two dates
You can do something like this:
month(date())-month({01/01/1999})
Mark

Quote:
>Is there a date difference function offered by FoxPro 5.0 that will
>return to me the number of months difference between 2 dates. My dates
>are in either format MM/DD/YY or MM/DD/YYYY (they are defined as date
>fields in my database).

>Do I have to write my own? If I do if anyone has a function that
>handles this already that would be great too. I am sure I can figure
>it out on my own too.
>--
>Laura Erskine. To reply remove STTYOPALL_ from email address.
>____



Sat, 12 Jan 2002 03:00:00 GMT  
 Way to figure out number of months difference between two dates
The problem you run into with this solution is when you are spanning years.
If you subtract the month function of 07/27/1998 from the month fcn of
today's date, you will wind up with 0 because both  fcns evaluate to 7.

A more precise way to do it would be to use straight date subtraction,
divide by 365.25 (the .25 accounts for leap years) to get the number of
years, and then multiply by 12 to get the # of months.

ie:

** Calculate number of months elapsed

dBirthday = CTOD("01/01/1945")
dToday = DATE()
nMonths = (dToday - dBirthday) / 365.25 * 12
? nMonths

** End of prg

Assuming today is 07/27/1999, the resulting output is 654.7844

--
When sending mail, please remove the spam filter (spamless.) from my
address.
Thank you.


Quote:
> You can do something like this:
> month(date())-month({01/01/1999})
> Mark


> >Is there a date difference function offered by FoxPro 5.0 that will
> >return to me the number of months difference between 2 dates. My dates
> >are in either format MM/DD/YY or MM/DD/YYYY (they are defined as date
> >fields in my database).

> >Do I have to write my own? If I do if anyone has a function that
> >handles this already that would be great too. I am sure I can figure
> >it out on my own too.
> >--
> >Laura Erskine. To reply remove STTYOPALL_ from email address.
> >____



Sat, 12 Jan 2002 03:00:00 GMT  
 Way to figure out number of months difference between two dates
Laura,
You have to write a function to get that information.  I have a need to know
how many months a date range touches.  I wrote a function to do this.  For
instance a date range of 7/30/99 - 8/01/99 needed to return 2 for my
application.  You can tweak this to make it fit your needs.

**************
** Purpose: return the number of months in a given date range
LPARAMETERS ldStartDate, ldStopDate

LOCAL lnMos, ldHoldDate
lnMos = 0
ldHoldDate = ldStartDate - DAY(ldStartDate) + 1
DO WHILE ldHoldDate <= ldStopDate
 ldHoldDate = GOMONTH(ldHoldDate,1)
 lnMos = lnMos + 1
ENDDO

RETURN lnMos
*************************

HTH
Cathy
--
Cathy Ciciulla Neppel
Omaha, NE
cneppel *AT* tmvgas.com

Quote:
>Is there a date difference function offered by FoxPro 5.0 that will
>return to me the number of months difference between 2 dates. My dates
>are in either format MM/DD/YY or MM/DD/YYYY (they are defined as date
>fields in my database).

>Do I have to write my own? If I do if anyone has a function that
>handles this already that would be great too. I am sure I can figure
>it out on my own too.
>--
>Laura Erskine. To reply remove STTYOPALL_ from email address.
>____



Sat, 12 Jan 2002 03:00:00 GMT  
 Way to figure out number of months difference between two dates
Laura,

Here's a function that will return the number of months between two dates.

* Months.prg
LPARAMETERS pdDate1, pdDate2
IF pdDate1 = pdDate2
    RETURN 0
ENDIF
LOCAL ldDate1, ldDate2, lnMonths
lnMonths = 0
* get the latest date
ldDate1 = MAX(pdDate1,pdDate2)
* get the earlier date
ldDate2 = MIN(pdDate1,pdDate2)

* Now count the months
DO WHILE ldDate1 > lddate2
       lnMonths = lnMonths + 1
       ldDate2 = GOMONTH(ldDate2,1)
ENDDO
RETURN lnMonths
* End of function

This removes any approximations that 325.25 places on things.  It will
correctly deal with leap years and it will calculate correctly across years.

--
JimB
www.jamesbooth.com



Sat, 12 Jan 2002 03:00:00 GMT  
 Way to figure out number of months difference between two dates
You mean something like this?

LPARAMETERS tdDate1, tdDate2
DO CASE
 CASE tdDate1 > tdDate2
 *if the 1st date is after the 2nd
  lnMonthDif = 12 * (YEAR(tdDate1) - YEAR(tdDate2))
  lnReturn = lnMonthDif + MONTH(tdDate1) - MONTH(tdDate2)
 CASE tdDate1 < tdDate2
 *if the 1st date is before the 2nd
  lnMonthDif = 12 * (YEAR(tdDate2) - YEAR(tdDate1))
  lnReturn = lnMonthDif + MONTH(tdDate2) - MONTH(tdDate1)
 OTHERWISE
  * both dates are equal
  lnReturn = 0
ENDCASE
RETURN lnReturn

Greetings,
Sietse

Quote:
>Is there a date difference function offered by FoxPro 5.0 that will
>return to me the number of months difference between 2 dates. My dates
>are in either format MM/DD/YY or MM/DD/YYYY (they are defined as date
>fields in my database).

>Do I have to write my own? If I do if anyone has a function that
>handles this already that would be great too. I am sure I can figure
>it out on my own too.
>--
>Laura Erskine. To reply remove STTYOPALL_ from email address.
>____



Sun, 13 Jan 2002 03:00:00 GMT  
 Way to figure out number of months difference between two dates
? (year(d2) - year(d1)) * 12 + month(d2) - month(d1)



Quote:
> Is there a date difference function offered by FoxPro 5.0 that will
> return to me the number of months difference between 2 dates. My dates
> are in either format MM/DD/YY or MM/DD/YYYY (they are defined as date
> fields in my database).

> Do I have to write my own? If I do if anyone has a function that
> handles this already that would be great too. I am sure I can figure
> it out on my own too.
> --
> Laura Erskine. To reply remove STTYOPALL_ from email address.
> ____



Sun, 13 Jan 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Calculating Number of Days and Number of Months between 2 dates

2. How to figure Average of days between two dates for a group

3. # months between two dates

4. How to determine the difference between two dates?

5. Date - Date To Find Number Of Days?

6. Difference between two tables

7. Number of days in specified month

8. How to extract the difference data records from two similar tables

9. Month End Date

10. Determining # of months between arbitrarry dates

11. Retrieving the month out of a date variable

12. Calculate date for End of Month from mmyy

 

 
Powered by phpBB® Forum Software