On Mar 27, 2007, at 6:14 PM, Bryant Minard wrote:

> If I have variable (d = 397, the difference between two dates), how  
> can I
> show that in years, months and days? (example 1 year, 1 month and 1  
> day)
> Has anyone written a function to do something like this?

This is one I adapted that does each section. It would be easy to  
write a shell over it to do what you want.

************************************************************************ 
*******************
************************************************************************ 
*******************
** Adapted by:
** Kenneth B. Kixmoeller
** Jaguar Marketing Services, Inc.
** 35 Birchwood Lane
** Birchwood Village, Minnesota  55110-1609 USA
** Telephone                    612/978-9124
** Facsimile                            651/653-7540
** Electronic Mail      [EMAIL PROTECTED]
** Web URL                                      www.Information-Architecture.com
************************************************************************ 
*****************
* VB.PRG => VBJaguar.PRG
* Procedure file with VB function equivalents
************************************************************************ 
*****************
************************************************************************ 
*****************
** Adapted from Miriam Liskin's code from FoxPro Advisor, 3/2000 by:
**
* Returns the result of subtracting two date/times, expressed in any  
date/time increment
* FUNCTION DateDiff
************************************************************************ 
*****************
************************************************************************ 
*****************
LPARAMETERS lcInterval, ldDateTime1, ldDateTime2, lnFirstDOW,  
lnFirstWeek
lcInterval = LOWER(lcInterval)
local lnReturn,ldDateTime1bk,ldDateTime2bk              && to back up the times
lnReturn = .null.


IF PARAMETERS() < 4
        lnFirstDOW = 1
ENDIF
IF INLIST(lcInterval, "h", "n", "s")
        * If time interval requested, convert Date to Date/Time
        IF VARTYPE(ldDateTime1) = "D"
                ldDateTime1 = DTOT(ldDateTime1)
        ENDIF   
        IF VARTYPE(ldDateTime2) = "D"
                ldDateTime2 = DTOT(ldDateTime2)
        ENDIF   
ELSE
        * Otherwise, convert DateTime to Date
        IF VARTYPE(ldDateTime1) = "T"
                ldDateTime1bk = ldDateTime1             && back up the original 
time values  
for later evaluation
                ldDateTime1 = TTOD(ldDateTime1)
        ENDIF   
        IF VARTYPE(ldDateTime2) = "T"
                ldDateTime2bk = ldDateTime2             && back up the original 
time values  
for later evaluation
                ldDateTime2 = TTOD(ldDateTime2)
        ENDIF   

ENDIF
DO CASE
        CASE lcInterval = "yyyy"                        && Years
                lnReturn = YEAR(ldDateTime2) - YEAR(ldDateTime1)
                */** kbk: For my purposes, I was looking only for Whole years,  
therefore:
                if (lnReturn > 0) AND (MONTH(ldDateTime1) >= MONTH(ldDateTime2))
                        lnReturn = lnReturn -1
                endif

        CASE lcInterval = "q"                           && Quarters
                IF MONTH(ldDateTime2) >= MONTH(ldDateTime1)
                        lnMonths = (YEAR(ldDateTime2) - YEAR(ldDateTime1)) * 12 
+;
                                MONTH(ldDateTime2) - MONTH(ldDateTime1)
                ELSE
                        lnMonths = (YEAR(ldDateTime2) - YEAR(ldDateTime1) - 1) 
* 12 +;
                                MONTH(ldDateTime2) + 12 - MONTH(ldDateTime1)
                ENDIF
                lnReturn = INT(lnMonths / 3)

        CASE lcInterval = "m"                           && Months
                IF MONTH(ldDateTime2) >= MONTH(ldDateTime1)
                        lnReturn = (YEAR(ldDateTime2) - YEAR(ldDateTime1)) * 12 
+;
                                MONTH(ldDateTime2) - MONTH(ldDateTime1)
                ELSE
                        lnReturn = (YEAR(ldDateTime2) - YEAR(ldDateTime1) - 1) 
* 12 +;
                                MONTH(ldDateTime2) + 12 - MONTH(ldDateTime1)
                ENDIF   
                */** kbk: Ditto: For my purposes, I was looking only for Whole  
Months, therefore:
                if day(ldDateTime1) > day(ldDateTime2)
                        lnReturn = lnReturn - 1
                endif   

        CASE INLIST(lcInterval, "y", "d")       && Days
                lnReturn = ldDateTime2 - ldDateTime1
                */** kbk:Ditto, I was looking only for Whole Days, therefore:
                */**   when passing Times, losing the time forestalled the 
ability  
to determine whether
                */**   it was a whole day
                if vartype(ldDateTime1bk) == "T" AND vartype(ldDateTime2bk) == 
"T"               
&& We started with Time
                        if hour(ldDateTime1bk) > hour(ldDateTime2bk)            
&& the hour of the  
first one is bigger thatn the hour of the second (we haven't yet  
traversed a full day)
                                lnReturn = lnReturn - 1
                        endif
                endif

        CASE lcInterval = "ww"                          && Weekdays
                lnReturn = INT((ldDateTime2 - ldDateTime1) / 7)
                IF DOW(ldDateTime2, lnFirstDOW) < DOW(ldDateTime1, lnFirstDOW) 
-1
                        lnReturn = lnReturn + 1
                ENDIF

        CASE lcInterval = "w"                           && Weeks
                lnReturn = INT((ldDateTime2 - ldDateTime1) / 7)

        CASE lcInterval = "h"                           && Hour
                lnReturn = INT((ldDateTime2 - ldDateTime1) / 3600)

        CASE lcInterval = "n"                           && Minute
                lnReturn = round((ldDateTime2 - ldDateTime1) / 60,0)
                *lnReturn = INT((ldDateTime2 - ldDateTime1) / 60)

        CASE lcInterval = "s"                           && Second
                lnReturn = ldDateTime2 - ldDateTime1

ENDCASE
RETURN lnReturn
  


_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/profox
OT-free version of this list: http://leafe.com/mailman/listinfo/profoxtech
** All postings, unless explicitly stated otherwise, are the opinions of the 
author, and do not constitute legal or medical advice. This statement is added 
to the messages for those lawyers who are too stupid to see the obvious.

Reply via email to