Date Difference 
Author Message
 Date Difference

I am trying to get an accurate difference between two dates to show in a
nicely displayed format.
E.g. 50 years 2 months 1 day
Does anybody know of a good formula for this calculation written in FoxPro
code?

Thanks In Advance
John R.



Wed, 21 Sep 2005 01:04:21 GMT  
 Date Difference
Hey John,

Just threw this together, but seems to work...

LPARAMETERS LoDate, HiDate

LoYearDays = LoDate - CTOD( "01/01/" + STR( YEAR( LoDate ), 4 ) )
HiYearDays = HiDate - CTOD( "01/01/" + STR( YEAR( HiDate ), 4 ) )
FullYears = YEAR( HiDate ) - YEAR( LoDate ) - IIF( LoYearDays > HiYearDays,
1, 0 )
FullMonths = (12 + MONTH( HiDate ) - MONTH( LoDate ) - IIF( DAY( LoDate ) >
DAY( HiDate ), 1, 0 )) % 12
IF MONTH( HiDate ) # MONTH( LoDate ) OR DAY( HiDate ) <= DAY( LoDate )
 Days = DAY ( HiDate ) + ( (GOMONTH( CTOD( PADL( MONTH(LoDate), 2, "0" ) +
"/01/" + ;
      STR( YEAR( LoDate ), 4 ) ), 1 ) - 1) - LoDate )
ELSE
 Days = DAY( HiDate ) - DAY( LoDate )
ENDIF

RETURN STR( FullYears, 4 ) + " years " + ALLTRIM( STR( FullMonths ) ) + "
months " + ;
  ALLTRIM( STR( Days ) ) + " days"

HTH,

John


Quote:
> I am trying to get an accurate difference between two dates to show in a
> nicely displayed format.
> E.g. 50 years 2 months 1 day
> Does anybody know of a good formula for this calculation written in FoxPro
> code?

> Thanks In Advance
> John R.



Wed, 21 Sep 2005 03:20:32 GMT  
 Date Difference
Using the same approach as the TimeDiff code given in a posting below:

*---------------------------------------------------------------------

*---------------------------------------------------------------------
* Example: years, months and days since my birthday
*    STORE 0 TO yy,mm,dd

*    ? yy,mm,dd
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Request:
*    da{*filter*}d  AS Date     OPTIONAL BY VALUE     DEFAULT DATE()
*    dateIni  AS Date     OPTIONAL BY VALUE     DEFAULT DATE()
*    nYears   AS Integer  OPTIONAL BY REFERENCE
*    nMonths  AS Integer  OPTIONAL BY REFERENCE
*    nDays    AS Integer  OPTIONAL BY REFERENCE
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Response:
*    nAllDays AS Integer
*---------------------------------------------------------------------
FUNCTION DateDiff
* Preconditions
    LPARAMETERS da{*filter*}d, dateIni, nYears, nMonths, nDays
    PRIVATE ALL
    STORE 0 TO nYears, nMonths, nDays, nAllDays

    defaultDate = DATE()
    IF VARTYPE(dateIni)=="T"  THEN
        dateIni = TTOD(dateIni)        && Convert to Date
    ENDIF
    IF VARTYPE(da{*filter*}d)=="T"  THEN
        da{*filter*}d = TTOD(da{*filter*}d)        && Convert to Date
    ENDIF
    IF EMPTY(dateIni) OR VARTYPE(dateIni)<>"D"  THEN
        dateIni = defaultDate        && Assign default value
    ENDIF
    IF EMPTY(da{*filter*}d) OR VARTYPE(da{*filter*}d)<>"D" THEN
        da{*filter*}d = defaultDate        && Assign default value
    ENDIF
    IF PARAMETERS() < 3 THEN        && Return days only
        RETURN da{*filter*}d-dateIni
    ENDIF
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Calculations
    nAllDays = da{*filter*}d-dateIni

    isNegative = nAlldays < 0
    IF isNegative THEN    && swap dates
        dateTmp = dateIni
        dateIni = da{*filter*}d
        da{*filter*}d = dateTmp
    ENDIF

    yyIni = YEAR(dateIni)
    mmIni = MONTH(dateIni)
    ddIni = DAY(dateIni)

    yyEnd = YEAR(da{*filter*}d)
    mmEnd = MONTH(da{*filter*}d)
    ddEnd = DAY(da{*filter*}d)

    * Get years
    nYears = yyEnd-yyIni
    IF mmIni > mmEnd OR (mmIni = mmEnd AND ddIni > ddEnd) THEN
        nYears = nYears - 1
    ENDIF
    * Get months
    IF mmEnd > mmIni OR (mmEnd = mmIni AND ddEnd >= ddIni) THEN
        nMonths = mmEnd-mmIni
    ELSE
        nMonths = 12-mmIni + mmEnd
    ENDIF
    * Get days
    IF ddIni > ddEnd THEN
        nMonths = nMonths - 1
        nDays = (GOMONTH(DATE(yyIni,mmIni,01),1) - dateIni) + ddEnd - 1
    ELSE
        nDays = ddEnd-ddIni
    ENDIF

    IF isNegative THEN
        nYears = -nYears
    ENDIF

    RETURN nAllDays
ENDFUNC
*---------------------------------------------------------------------


Quote:
> I am trying to get an accurate difference between two dates to show in a
> nicely displayed format.
> E.g. 50 years 2 months 1 day
> Does anybody know of a good formula for this calculation written in FoxPro
> code?

> Thanks In Advance
> John R.



Wed, 21 Sep 2005 04:56:09 GMT  
 Date Difference
Hi John

You can use the date maths functions
d1=DATE(2003,04,05)
d2=DATE(1955,04,06)
d3= DATE(1900,1,1)
d4= d3+(MAX(d1,d2)-MIN(d1,d2))
 ? year(d4)-1900,MONTH(d4)-1, DAY(d4)-1

47 years,11 months, 30 days

or written as a function
Function CalcTime (d1, d2)
Local d4,y,m, d
d4=DATE(1700,1,1)+(MAX(d1,d2)-MIN(d1,d2))
y=Year(M.d4)-1700
m=Month(M.d4)-1
d=Day(M.d4)-1
Return TRANSFORM(M.y)+' years, '+trans(M.m)+' months, '+ trans(M.d)+' days'

Using GetWordNum(expr,n,',') you can extract the parts of the string

-Anders


Quote:
> I am trying to get an accurate difference between two dates to show in a
> nicely displayed format.
> E.g. 50 years 2 months 1 day
> Does anybody know of a good formula for this calculation written in FoxPro
> code?

> Thanks In Advance
> John R.



Wed, 21 Sep 2005 17:56:45 GMT  
 Date Difference
nice approach Anders, the simplest but not the most accurate, if you try
this:

d1={^2003/07/01}    && from july 1st
d2={^2003/08/31}    && to august 31st  =>  1 month and 30 days => 60 days
d3={^1900/1/1}
d4= d3+(MAX(d1,d2)-MIN(d1,d2))
? year(d4)-1900,MONTH(d4)-1, DAY(d4)-1

result = 0 years 2 months 2 days
as you can see, february can mess up the result as it has 28 days only


Quote:
> Hi John

> You can use the date maths functions
> d1=DATE(2003,04,05)
> d2=DATE(1955,04,06)
> d3= DATE(1900,1,1)
> d4= d3+(MAX(d1,d2)-MIN(d1,d2))
>  ? year(d4)-1900,MONTH(d4)-1, DAY(d4)-1

> 47 years,11 months, 30 days

> or written as a function
> Function CalcTime (d1, d2)
> Local d4,y,m, d
> d4=DATE(1700,1,1)+(MAX(d1,d2)-MIN(d1,d2))
> y=Year(M.d4)-1700
> m=Month(M.d4)-1
> d=Day(M.d4)-1
> Return TRANSFORM(M.y)+' years, '+trans(M.m)+' months, '+ trans(M.d)+'
days'

> Using GetWordNum(expr,n,',') you can extract the parts of the string

> -Anders



> > I am trying to get an accurate difference between two dates to show in a
> > nicely displayed format.
> > E.g. 50 years 2 months 1 day
> > Does anybody know of a good formula for this calculation written in
FoxPro
> > code?

> > Thanks In Advance
> > John R.



Sun, 25 Sep 2005 08:14:04 GMT  
 Date Difference
Hi George
You're right (though it's 61 days). WE'll have vto define rhe rules for one
thing.
Between d1={^2003/07/30}   and {^2003/08/30}   there are 31 days (d1-d1) ,
or is it 1 month 0 days (GOMONTH({^2003/08/30} , -1), or is it 1 month and 1
day?
-Anders


Quote:
> nice approach Anders, the simplest but not the most accurate, if you try
> this:

> d1={^2003/07/01}    && from july 1st
> d2={^2003/08/31}    && to august 31st  =>  1 month and 30 days => 60 days
> d3={^1900/1/1}
> d4= d3+(MAX(d1,d2)-MIN(d1,d2))
> ? year(d4)-1900,MONTH(d4)-1, DAY(d4)-1

> result = 0 years 2 months 2 days
> as you can see, february can mess up the result as it has 28 days only



> > Hi John

> > You can use the date maths functions
> > d1=DATE(2003,04,05)
> > d2=DATE(1955,04,06)
> > d3= DATE(1900,1,1)
> > d4= d3+(MAX(d1,d2)-MIN(d1,d2))
> >  ? year(d4)-1900,MONTH(d4)-1, DAY(d4)-1

> > 47 years,11 months, 30 days

> > or written as a function
> > Function CalcTime (d1, d2)
> > Local d4,y,m, d
> > d4=DATE(1700,1,1)+(MAX(d1,d2)-MIN(d1,d2))
> > y=Year(M.d4)-1700
> > m=Month(M.d4)-1
> > d=Day(M.d4)-1
> > Return TRANSFORM(M.y)+' years, '+trans(M.m)+' months, '+ trans(M.d)+'
> days'

> > Using GetWordNum(expr,n,',') you can extract the parts of the string

> > -Anders



> > > I am trying to get an accurate difference between two dates to show in
a
> > > nicely displayed format.
> > > E.g. 50 years 2 months 1 day
> > > Does anybody know of a good formula for this calculation written in
> FoxPro
> > > code?

> > > Thanks In Advance
> > > John R.



Sun, 25 Sep 2005 21:08:45 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. date difference

2. Date differences between VFP 5 and VFP 6

3. Date Difference function

4. Date Time differences?

5. Way to figure out number of months difference between two dates

6. date time type differences between vfp5 and vfp6

7. How to determine the difference between two dates?

8. Dates and Date Ranges

9. Record Set Date Compare to Mem Var Date

10. Date - Date To Find Number Of Days?

11. SQL Question, Max Date (Nearest max date)

12. VFP BUG: GOMONTH() returns blank date for dates prior to {01/01/1753}

 

 
Powered by phpBB® Forum Software