Difference between dates

Posted on 2012-08-30
Last Modified: 2012-08-31
Having 30 Aug 2012 19:15:00 and 30 Aug 2012 19:15:45 how can I calculate the difference between the two dates in the format hh:mm:ss
I'm having trouble getting the date recognised as a date to do the datetime.parse.

Question by:Gary
    LVL 85

    Accepted Solution

    You'll need to use DateTime.TryParseExact(), and specify a culture of English so it can recognize the short forms of the months.  Also note that the maximum value that can be returned by the Hours() property is 23 so if the difference is greater than that you'll need to include the Days() property into your format:
            Dim strDtA As String = "30 Aug 2012 19:15:00"
            Dim strDtB As String = "30 Aug 2012 19:15:45"
            Dim DtA, DtB As DateTime
            Dim English As New System.Globalization.CultureInfo("en-US")
            Dim AllowedFormats() As String = {"d MMM yyyy H:mm:ss", "dd MMM yyyy H:mm:ss", "d MMM yyyy HH:mm:ss", "dd MMM yyyy HH:mm:ss"}
            If DateTime.TryParseExact(strDtA, AllowedFormats, English, Globalization.DateTimeStyles.None, DtA) Then
                If DateTime.TryParseExact(strDtB, AllowedFormats, English, Globalization.DateTimeStyles.None, DtB) Then
                    Dim TS As TimeSpan = DtB.Subtract(DtA)
                    Dim strTimeSpan As String = String.Format("{0}:{1}:{2}", TS.Hours.ToString("00"), TS.Minutes.ToString("00"), TS.Seconds.ToString("00"))
                    MessageBox.Show(strTimeSpan, "Duration")
                    MessageBox.Show(strDtB, "Invalid Stop Date")
                End If
                MessageBox.Show(strDtA, "Invalid Start Date")
            End If

    Open in new window

    LVL 77

    Expert Comment

    by:David Johnson, CD, MVP
    Is it a string or a date object? you might have to do the conversion.
    LVL 58

    Author Closing Comment

    Works great, thanks

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now