locked
Time difference in vb.net

    Question

  • Hi all,
    I'm having the situation to calculate as follows: The time difference between 4:00pm and 1:00 am is 9 hours. This is derived by subtracting 4 from 12 which equals 8 and adding 1 hour for the time between 12:00 and 1:00. I'm having 2 datetimepicker in starttime i've given 4:00 pm and endtime i've given 1:00 am..i'm having starttime as datetime and endtime as datetime.. How to get the time difference in hours.. In this i've kept datetimepicker format as custom and custom format as "HH:mm:ss"..
    Thanks in advance
    Keen to learn
    Thursday, July 03, 2008 8:32 AM

Answers

  • use the datdiff function, an example :-

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim x As Date

    Dim y As Date

    x = DateTime.Now

    System.Threading.Thread.Sleep(5000)

    y = DateTime.Now

    Dim SecondsDifference As Integer

    SecondsDifference = DateDiff(DateInterval.Second, x, y)

    MessageBox.Show("Difference in seconds is : " & SecondsDifference.ToString())

    End Sub

    Thursday, July 03, 2008 9:07 AM
  • Hi rk4088.  It can be simpler than DateDiff even.  Whenever you perform math on two Date value teh experssion results in a DateTime variable that has built in functions to determine total number of seconds, minutes, hours, etc..


    Dim d1 As Date = Now
    Dim d2 As Date = d1.AddHours(15)
    MessageBox.Show(
    "Difference in seconds is : " & (d2 - d1).TotalSeconds)

    Thursday, July 03, 2008 11:31 AM
  • Hi,

    Try this with one button on a FORM please.>>

    Shows you two ways of getting 9 hours difference as a result.>>




    Regards,

    John
    ___________________________________________________




    Option Strict On

    Public Class Form1

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

    My.Application.ChangeCulture("en-US")
    'June 20th at 1am.>>
    Dim dt2 As DateTime = Convert.ToDateTime("06/20/2008 01:00:00")
    'June 19th at 16:00 or 4pm.>>
    Dim dt1 As DateTime = Convert.ToDateTime("06/19/2008 16:00:00")

    'Use a TimeSpan like this.>>
    Dim ts As New TimeSpan
    ts = dt2.Subtract(dt1)
    MessageBox.Show("Hours difference = " & ts.Hours.ToString)

    'or use DateDiff like this.>>
    Dim diff As Long = DateDiff(DateInterval.Hour, dt2, dt1)
    MessageBox.Show("Hours difference = " & ts.Hours.ToString)

    End Sub
    End Class


    I have previously been, until recently, an MSP ( Microsoft Student Partner ).
    Thursday, July 03, 2008 1:36 PM

All replies

  • use the datdiff function, an example :-

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim x As Date

    Dim y As Date

    x = DateTime.Now

    System.Threading.Thread.Sleep(5000)

    y = DateTime.Now

    Dim SecondsDifference As Integer

    SecondsDifference = DateDiff(DateInterval.Second, x, y)

    MessageBox.Show("Difference in seconds is : " & SecondsDifference.ToString())

    End Sub

    Thursday, July 03, 2008 9:07 AM
  • Hi rk4088.  It can be simpler than DateDiff even.  Whenever you perform math on two Date value teh experssion results in a DateTime variable that has built in functions to determine total number of seconds, minutes, hours, etc..


    Dim d1 As Date = Now
    Dim d2 As Date = d1.AddHours(15)
    MessageBox.Show(
    "Difference in seconds is : " & (d2 - d1).TotalSeconds)

    Thursday, July 03, 2008 11:31 AM
  • You will need to use the date as well as the time to get the calculation to work correctly.

    #1:00# - #16:00# = -15:00 hours

    #7-4-2008 1:00# - #7-3-2008 16:00# = 9:00 hours

    Thursday, July 03, 2008 12:04 PM
  • Hi,

    Try this with one button on a FORM please.>>

    Shows you two ways of getting 9 hours difference as a result.>>




    Regards,

    John
    ___________________________________________________




    Option Strict On

    Public Class Form1

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

    My.Application.ChangeCulture("en-US")
    'June 20th at 1am.>>
    Dim dt2 As DateTime = Convert.ToDateTime("06/20/2008 01:00:00")
    'June 19th at 16:00 or 4pm.>>
    Dim dt1 As DateTime = Convert.ToDateTime("06/19/2008 16:00:00")

    'Use a TimeSpan like this.>>
    Dim ts As New TimeSpan
    ts = dt2.Subtract(dt1)
    MessageBox.Show("Hours difference = " & ts.Hours.ToString)

    'or use DateDiff like this.>>
    Dim diff As Long = DateDiff(DateInterval.Hour, dt2, dt1)
    MessageBox.Show("Hours difference = " & ts.Hours.ToString)

    End Sub
    End Class


    I have previously been, until recently, an MSP ( Microsoft Student Partner ).
    Thursday, July 03, 2008 1:36 PM
  • Hi,

    How do you choose the time with a DateTimePicker?

    Anyway, this will show the TOTAL HOURS difference between
    two    DateTimePickers on a FORM.>>




    Regards,

    John
    ______________________________________________


    Option Strict On

    Public Class Form1

    Private Sub DateTimePickers_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged, DateTimePicker2.ValueChanged

    Dim ts As TimeSpan = DateTimePicker1.Value.Subtract(DateTimePicker2.Value)
    Dim TotalHours As Double = ts.TotalHours
    MessageBox.Show("Hours difference is " & TotalHours.ToString)

    End Sub
    End Class


    I have previously been, until recently, an MSP ( Microsoft Student Partner ).
    Thursday, July 03, 2008 1:49 PM