By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

Time calculations

jamesd0142
100+
P: 469
Ok i have bashed my head against a wall trying to work out how this is done... please help.

i have 2 text boxes with the values:

txt1.text = "15:27:06"
txt2.text = "1:05:27"

Now if txt2.text = the current time and i want the time when it was txt1.text (15hours,27mins,6seconds) ago how do i do it...

for example current time is "01:05:27" and if i subtract "15:27:06" away i should get time as "09:38:31"

any1 able to help?
May 3 '10 #1
Share this Question
Share on Google+
6 Replies


tlhintoq
Expert 2.5K+
P: 3,525
Look at the MSDN for the DateTime object.

You can subtract one DateTime from another DateTime, then take the .ToString() of the result.
May 4 '10 #2

jamesd0142
100+
P: 469
Static LOF As DateTime = "15:27:06"
Static RT As DateTime = "1:05:27"

MsgBox(RT.Subtract(LOF).ToString)

answer = "-14:19:38"
RUBBISH :(

----------------------
if its "1:05:27" and u go back "15:27:06" i should get "09:38:31"

so im really confused?
May 4 '10 #3

tlhintoq
Expert 2.5K+
P: 3,525
If you don't specify the date, then both times are on the same date. Therefore the math woks.

There is no assumption made that 0100 is on Tuesday and 1500 is on Monday.
Computers don't assume.

You didn't go read up on the DateTIme object in the MSDN did you? You just started using it with a "let's see if this works" mindset. Kinda like when a 4 year old sticks a fork in an outlet: I wonder what this will do?
May 4 '10 #4

jamesd0142
100+
P: 469
@tlhintoq
No your right... i did just jump in without reading.
ill do a little more reading :) thanks for the help
May 4 '10 #5

Frinavale
Expert Mod 5K+
P: 9,731
You can use one of the DateTime.TryParse() methods to parse a string into a DateTime Structure which can be used to work with Times. Check out the DateTime structure link that I posted for examples on how to work with DateTime objects.
May 4 '10 #6

P: 4
James already gave the right asnwer but he was a little confused:

Static LOF As DateTime = "15:27:06"
Static RT As DateTime = "1:05:27"
MsgBox(LOF.Subtract(RT).ToString)

you can also replase the times with:
Static LOF As DateTime = textbox1.text
Static RT As DateTime = textbox2.text
MsgBox(LOF.Subtract(RT).ToString)
May 8 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.