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

Recommended Answers

All 3 Replies

Hi!

This is what I did. I'm writing this @ 14:25 and hence added some hours to simulate your situation. I'm directly taking the datetime but you can do the same from the datetimepicker.

Public Class Form1
    Dim d1, d2, d3 As DateTime

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        d1 = Now.AddHours(2)
        d2 = Now.AddHours(11)
        Me.TextBox1.Text = d1
        Me.TextBox2.Text = d2
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        d3 = (d2 - d1).ToString
        Me.TextBox3.Text = d3
    End Sub

    
End Class

Alternatively, you can use a Timespan object. I guess this is more elegant-

Public Class Form1
    Dim d1, d2 As DateTime
    Dim x As TimeSpan

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        d1 = Now.AddHours(2)
        d2 = Now.AddHours(11)
        Me.TextBox1.Text = d1
        Me.TextBox2.Text = d2
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        x = d2 - d1
        Me.TextBox3.Text = x.ToString
    End Sub

    
End Class

nice codes

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.