I have two variables :
Dim starttime As TimeSpan
Dim endtime As TimeSpan
My starttime value is : 02:30:00 (I mean 2.30AM)
2.30AM is nex
You need to use a DateTime variable to hold your start time and end time. Like this:
Dim startTime As New DateTime(2013, 9, 19, 10, 30, 0) ' 10:30 AM today
Dim endTime As New DateTime(2013, 9, 20, 2, 0, 0) ' 2:00 AM tomorrow
Dim duration As TimeSpan = endTime - startTime 'Subtract start time from end time
Console.WriteLine(duration)
Result:
15:30:00
UPDATE:
To convert that result to minutes, you can use the TotalMinutes property of the TimeSpan variable:
Console.WriteLine(duration.TotalMinutes)
Result:
930
You're confusing DateTime and TimeSpan. TimeSpan stores a duration, therefore anything about AM and PM is not relevant. If you want to compare two times, you should use DateTime and subtract them both, which will give you a TimeSpan.
You cannot give a TimeSpan a value of '2am', you you should use DateTime for that.
Consider:
var date1 = DateTime.Now.Date.AddHours(2); // pseudo code 2am
var date2 = DateTime.Now.Date.AddHours(11); // pseudo code 11am
var result = date2 - date1;
The result here is going to be a duration of 9 hours.
If you want it to be 2am the following day, you should include AddDays(1);
var date1 = DateTime.Now.Date.AddDays(1).AddHours(2); // pseudo code 2am the next day
var date2 = DateTime.Now.Date.AddHours(11); // pseudo code 11am
var result = date1 - date2;
The result here is going to be 15 hours.
HERE IS MY SOLUTION FOR DIFFERENCE IN HOURS
'WORKS ONLY TIME SPAN WITHIN 48 HRS
Dim HRS As TimeSpan
Dim St As TimeSpan = TimeSpan.Parse(L_A_START.Text)
Dim Cl As TimeSpan = TimeSpan.Parse(L_A_CLOSE.Text)
HRS = Cl - St
If HRS.Hours <= 0 Then
HRS = (HRS + New TimeSpan(0, 24, 0, 0, 0))
End If
L_A_HRS.Text = HRS.ToString()