VB.Net Difference Between Two Dates

How To Get Timespan Difference Between Two Datetime Using VB.Net


Difference Between 2 Dates In VB.Net



In this VB.NET Tutorial we will see How To Get The Difference Between 2 Dates In Days, Hours, Minutes, Seconds, Milliseconds On A Button Click Event In Visual Basic.Net  Programming Language And Visual  Studio Editor.




Project Source Code:


Public Class difference_between_two_dates

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        ' get the first date
        Dim date1 As DateTime = DateTimePicker1.Value

        ' get the second date
        Dim date2 As DateTime = DateTimePicker2.Value

        Dim dateDiff As TimeSpan = date2 - date1

        ' difference in days
        TextBoxDays.Text = dateDiff.TotalDays.ToString()

        ' difference in hours
        TextBoxHours.Text = dateDiff.TotalHours.ToString()

        ' difference in minutes
        TextBoxMinutes.Text = dateDiff.TotalMinutes.ToString()

        ' difference in seconds
        TextBoxSeconds.Text = dateDiff.TotalSeconds.ToString()

        ' difference in milliseconds
        TextBoxMilliseconds.Text = dateDiff.TotalMilliseconds.ToString()


    End Sub
End Class


OutPut:

2 datetime difference in vb.net




Share this

Related Posts

Previous
Next Post »