tags:

views:

103

answers:

2

i have a variable [ Dim mytime = "12:30:00 AM" ]

i want to subtract mytime from TimeOfDay and i also want to get the difference as a integer value... how should i do that..

i have added the following code

Dim mytime = DateTime.Parse("1:16:00 AM")
Dim result = TimeOfDay - mytime

dim finalresult = result.Seconds

but it is giving me negative values

+2  A: 

Easy:

First, convert the myTime to a DateTime – either directly:

Dim myTime = #12:30:00 AM#

Or by parsing a string:

Dim myTime = DateTime.Parse("12:30:00 AM")

Dim result = DateTime.TimeOfDay - myTime

But you won’t get an integer – how should this work? What should the integer represent? Instead, you get a TimeSpan.

If you need to have the difference in seconds, you can extract that, of course:

Dim differenceInSeconds = result.Seconds

Same goes for minutes or any other component.

Konrad Rudolph
but it is giving me the following resultOperator '-' is not defined for types 'String' and 'Date'.
Web Worm
@testkhan: Why String? I though you wanted to subtract two `DateTime`s. Ah, I get it … see my updated answer.
Konrad Rudolph
A: 
Dim difference As TimeSpan = DateTime.TimeOfDay - DateTime.Parse(mytime)
Justin Niessner
The result of subtracting two `DateTime` s is a `TimeSpan`, not a `DateTime` itself.
Konrad Rudolph
@Red-nosed unicorn Whoops...my mistake. Updated, fixed the order of subtraction, and got rid of my C#-y semi-colon.
Justin Niessner