Working out the number of hours between two dates?

  • Thread starter Thread starter Wayne Taylor
  • Start date Start date
W

Wayne Taylor

I want to work out how many hours there is between a start time and a finish
time.

For example

Start Time: 10:00 am
Finish Time: 11:00 am

Is 1 hour?

How can this be achieved using VB.NET?
 
DateTime dt1 = new DateTime(2004,01,01,12,0,0,0);
DateTime dt2 = new DateTime(2004,01,01,14,0,0,0);
TimeSpan ts = dt2-dt1;
Console.WriteLine( dt1.ToString() );
Console.WriteLine( dt2.ToString() );
Console.WriteLine( ts.ToString() ) ;
 
Use the System.TimeSpan:


Dim start As DateTime = DateTime.Now;
Dim end As DateTime = start + New TimeSpan(1, 0, 0);
Dim span As TimeSpan = end - start
Console.WriteLine(span.Hours)
 
Back
Top