Subtracting one time from another in vb.net
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 an integer value ... how should I do it.
I added the following code
Dim mytime = DateTime.Parse("1:16:00 AM")
Dim result = TimeOfDay - mytime
dim finalresult = result.Seconds
but it gives me negative values
+2
source to share
3 answers
Easy:
First convert myTime
to DateTime
- either directly:
Dim myTime = #12:30:00 AM#
Or by parsing the string:
Dim myTime = DateTime.Parse("12:30:00 AM")
Dim result = DateTime.TimeOfDay - myTime
But you won't get an integer - how is that supposed to work? What should an integer represent? You will get instead TimeSpan
.
If you need to have a difference in seconds, you can extract this, of course:
Dim differenceInSeconds = result.Seconds
The same goes for minutes or any other component.
+4
source to share