locked
calculating the difference between two dates RRS feed

  • Question

  • hi! does anyone know how i can compute the difference between two dates?

    example:

    Date1 = 03.15.2007

    Date2 = 03.16.2008

    output should be : 0 Months 1 Day(s) 1 Year(s)

    tnx...

    Wednesday, March 21, 2007 3:14 AM

Answers

All replies

  • Hi,

    Check this link from Ramani's pages

    ** How to calculate the difference between two dates in Years, Months, Days ?

    http://winnersoft.coolfreepages.com/vfp/yearmonthday.htm

    There you'll find some other cool tips.

    Hope this helps

    Cesar

    Wednesday, March 21, 2007 3:36 AM
  • set date ameri
    set cent on
    set talk off

    clos all
    clear


    public fdate,sdate

    store ctod('03/15/2007') to fdate   &&store date to variables
    store ctod('03/16/2008') to sdate   &&store date to variables

    a =   year(fdate)                   &&assign year of fdate to variable
    b =   year(sdate)                   &&assign year of sdate to variable

    c = b - a                           &&find no. of years difference

    d = month(fdate)                    &&assign month of fdate to variable
    e = month(sdate)                    &&assign month of sdate to variable

    f = e - d                           &&find no. of months difference

    g = day(fdate)                      &&assign day of sdate to variable
    h = day(sdate)                      &&assign day of fdate to variable

    i = h - g                           &&find no. of days difference

     

    ? allt(str(f)) + " Months " + allt(str(i)) + " Days " + allt(str(c)) + " Years "

     

    Wednesday, March 21, 2007 4:55 AM
  • Thank you! I was able to derive the right output with the code. Thank you again very much.

     

    Wednesday, March 21, 2007 5:35 AM
  • when i was writing my own code, i also thought it was as simple as that, but that wont really give the right output.

    if you try to process it with these dates:

    date1 = 03.15.2007

    date2 = 02.13.2008

    the answer will be -1 months -2 days 1 years

    Wednesday, March 21, 2007 5:41 AM
  • you can have a if statement to determine which date to be substracted.

     

    i..e

    if fdate >= sdate

    *substract fdate - sdate

    else

    if fdate < sdate

    *substract sdate - fdate

    endif

    endif

     

     

    Wednesday, March 21, 2007 6:44 AM
  • a =   year(fdate)                   &&assign year of fdate to variable
    b =   year(sdate)                   &&assign year of sdate to variable

    c = b - a                           &&find no. of years difference

    d = month(fdate) &&assign month of fdate to variable
    e = month(sdate)                    &&assign month of sdate to variable

    f = e - d                           &&find no. of months difference

    If f  < 0
     C = C -1
     f = 12 + f
    Endif

    g = day(fdate)                      &&assign day of sdate to variable
    h = day(sdate)                      &&assign day of fdate to variable

    i = h - g                           &&find no. of days difference
     
    If i  < 0
     f = f - 1
     i = i + 30
     * Condition further can be put across for Exact days in month
    Endif

    ? allt(str(f)) + " Months " + allt(str(i)) + " Days " + allt(str(c)) + " Years "

     

    Wednesday, April 4, 2012 12:21 PM
  • Hi,

    with VFP9 I would do it like this:

    d1 = {01.01.2011}
    d2 = DATE()
    STORE 0 TO liJ , liM , liT
    
    DO WHILE d1 < d2
    	DO CASE 
    	CASE GOMONTH( d1 , 12 ) < d2
    		liJ = liJ + 1 
    		d1 = GOMONTH( d1 , 12 )
    	CASE GOMONTH( d1 , 1 ) < d2
    		liM = liM + 1 
    		d1 = GOMONTH( d1 , 1 )
    	OTHERWISE 
    		liT = liT + 1 
    		d1 = d1 + 1 
    	ENDCASE 
    ENDDO 
    
    ? [Time between ] + DTOC( d1 ) + [ and ] + DTOC( d2 )
    ?  TRANSFORM( liJ ) + [ Years ]
    ?? TRANSFORM( liM ) + [ Months ]
    ?? TRANSFORM( liT ) + [ Days ]


    Gruss / Best regards -Tom 010101100100011001010000011110000101001001101111011000110110101101110011

    Wednesday, April 4, 2012 1:35 PM
    Answerer