Sunday, May 15, 2011

How to find Difference between two time (Dates) using VB.NET

Difference between two different dates using VB.NET

DATEDIFF function will come handy to find the difference between a set of dates. The snippet given below gets the difference in seconds

Private Sub DateDiffExample()

        Dim dt1 As Date = New Date(2011, 5, 15, 10, 11, 2)
        Dim dt2 As Date = New Date(2011, 5, 15, 10, 12, 22)

        Dim dt3 As Long = DateDiff(DateInterval.Second, dt1, dt2)

    End Sub

You can tweak a bit to get for Minutes or Hours etc by

Private Sub DateDiffExample()

        Dim dt1 As Date = New Date(2011, 5, 15, 10, 11, 2)
        Dim dt2 As Date = New Date(2011, 5, 15, 10, 12, 22)

        Dim dt3 As Long = DateDiff(DateInterval.Minute, dt1, dt2)

    End Sub

Other useful links:

1 comment:

  1. A Demo Application To Find Time Diference in Am and Pm Dates.
    You can easily download it from here . Here is also code and explanation

    http://geeksprogrammings.blogspot.in/2013/07/program-to-find-date-difference-visual.html

    ReplyDelete