Results 1 to 7 of 7

Thread: Difference between two dates durations

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Difference between two dates durations

    Lets say I have a date and time that is 05-01-07 01:11:11 pm. I have another date and time that is 05-05-07 05:50:55 pm.

    How can I find the duration between both dates and times down to the exact seconds?

    I would like to get how many days in between and the remaining of hours, minutes and seconds.

    So, in this case it would be 4 days and something hour, minutes and seconds.

    Thank You
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  2. #2
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    Re: Difference between two dates durations

    DateDiff() should do the trick.

    Do a search on this forum for more information.
    - Use the thread tools to Mark your Thread as Resolved when your question is answered.
    - Please Rate my answers if they where helpful.

  3. #3
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Difference between two dates durations

    The DateDiff() Function should work.

  4. #4
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Difference between two dates durations

    just for a quick reference
    vb Code:
    1. Private Sub Command1_Click()
    2.     Dim myDate1 As Date
    3.     myDate1 = "01-05-07 01:11:11 pm"
    4.     Dim myDate2 As Date
    5.     myDate2 = "05-05-07 05:50:55 pm"
    6.     MsgBox DateDiff("d", myDate1, myDate2)
    7.     MsgBox DateDiff("h", myDate1, myDate2)
    8.     MsgBox DateDiff("N", myDate1, myDate2)
    9.     MsgBox DateDiff("S", myDate1, myDate2)
    10. End Sub
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: Difference between two dates durations

    Thanks guys!

    But this could not be right.

    Code:
        Dim myDate1 As Date
        myDate1 = "01-05-07 01:11:11 pm"
        Dim myDate2 As Date
        myDate2 = "05-05-07 05:50:55 pm"
        
        Dim strDifference As String
        strDifference = DateDiff("d", myDate1, myDate2) & " days - " & _
                        DateDiff("h", myDate1, myDate2) & " hours - " & _
                        DateDiff("n", myDate1, myDate2) & " minutes - " & _
                        DateDiff("s", myDate1, myDate2) & " seconds."
        
        MsgBox "The difference between mydate2 and mydate1 is: " & strDifference
        End
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Difference between two dates durations

    Oh, yes it could be and is..... but it's being misused..... I'm guessing the days part is probably OK.... but I'm betting that you took one look at the hours minutes and seconds and went WOAH!.... but it's right..... For instance, if you look at the datediff between "5/2/2007" and "5/3/2007" using days... you get 1. If you use the same dates, but look at hours.... you get 24... both are correct. If you look at minutes you get (24*60).... all of which are correct.... Now, if you want 5 days, 3 hours, 27 minutes and 6 seconds..... it gets a little more complicated.

    First get the number of days.... then get the number of total hours, then subtract from that (days * 24) ..... what's left will get you the hours.... and so on and so on down through seconds. Like I said it gets complicated realy fast.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Difference between two dates durations

    you can do it like this
    Code:
        Const sFormat As String = " \da\y\s hh \hour\s mm \mi\nute\s ss \se\co\n\d\s"
        Dim d1 As Date, d2 As Date
        
        d1 = #5/1/2007 1:11:11 PM#
        d2 = #5/5/2007 5:50:55 PM#
        
        Debug.Print Int(d2 - d1) & Format$(d2 - d1, sFormat)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width