Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get time difference between two timespan in vb.net

Tags:

vb.net

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 next day

My endTime Value is : 10:30:00(I mean 10.30Am)

I want to get time difference of these. So I have code like this:

Dim span3 As TimeSpan = starttime .Subtract(endtime ) 

Now I am getting span3 : 08:00:00

This is wrong answer. Actually I want to get 16:00:00. (this is the exact differnce between 2.30Am To 10.30 Am)

How I can calculate this?

like image 767
user2786971 Avatar asked Sep 19 '13 12:09

user2786971


1 Answers

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

like image 128
Chris Dunaway Avatar answered Oct 02 '22 11:10

Chris Dunaway