Time difference in vb.net

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 4
Reputation: rk4088 is an unknown quantity at this point 
Solved Threads: 0
rk4088 rk4088 is offline Offline
Newbie Poster

Time difference in vb.net

 
0
  #1
Jul 3rd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 169
Reputation: tuse is an unknown quantity at this point 
Solved Threads: 14
tuse's Avatar
tuse tuse is offline Offline
Junior Poster

Re: Time difference in vb.net

 
0
  #2
Jul 3rd, 2008
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.

  1.  
  2.  
  3. Public Class Form1
  4. Dim d1, d2, d3 As DateTime
  5.  
  6. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  7. d1 = Now.AddHours(2)
  8. d2 = Now.AddHours(11)
  9. Me.TextBox1.Text = d1
  10. Me.TextBox2.Text = d2
  11. End Sub
  12.  
  13. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  14. d3 = (d2 - d1).ToString
  15. Me.TextBox3.Text = d3
  16. End Sub
  17.  
  18.  
  19. End Class
My blog on .NET- http://dotnet.tekyt.info
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 169
Reputation: tuse is an unknown quantity at this point 
Solved Threads: 14
tuse's Avatar
tuse tuse is offline Offline
Junior Poster

Re: Time difference in vb.net

 
0
  #3
Jul 3rd, 2008
Alternatively, you can use a Timespan object. I guess this is more elegant-

  1.  
  2. Public Class Form1
  3. Dim d1, d2 As DateTime
  4. Dim x As TimeSpan
  5.  
  6. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  7. d1 = Now.AddHours(2)
  8. d2 = Now.AddHours(11)
  9. Me.TextBox1.Text = d1
  10. Me.TextBox2.Text = d2
  11. End Sub
  12.  
  13. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  14. x = d2 - d1
  15. Me.TextBox3.Text = x.ToString
  16. End Sub
  17.  
  18.  
  19. End Class
My blog on .NET- http://dotnet.tekyt.info
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2,641
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Solved Threads: 245
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: Time difference in vb.net

 
0
  #4
Jul 3rd, 2008
nice codes
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC