-
May 27th, 2010, 07:05 AM
#1
Thread Starter
New Member
Subtracting time values..HELP!
Hi all, I am building a program as my college project. The project will later be implemented into a snooker hall. The program has to generate a start and a stop time, then the difference should be calculated and a cost for the time played should be generated. the cost will be £3 to every hour. So far i have managed to generate the two different times when the start and stop buttons are pressed. I will insert the code that I already have and if someone could help i would be very greatful! thanks!
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Button1.BackColor = Color.Red
Button1.Text = "In Use"
Dim startTime As Date
startTime = TimeOfDay
TextBox1.Text = startTime
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Button1.BackColor = Color.ForestGreen
Button1.Text = "Table 1"
Dim stopTime As Date
stopTime = TimeOfDay
TextBox2.Text = stopTime
End Sub
-
May 27th, 2010, 07:48 AM
#2
Re: Subtracting time values..HELP!
You are working with .Net so I'm not sure, but in VB classic there is a DateDiff function which can calculate the difference between two date/times. You can choose the interval in which you want the results, years, months, days, hours, minutes, seconds.
P.S: It seems to be available in .Net too, Googled "DateDiff in .Net" and got this http://msdn.microsoft.com/en-us/libr...6f(VS.80).aspx
-
May 27th, 2010, 08:07 AM
#3
Thread Starter
New Member
Re: Subtracting time values..HELP!
Thanks for your input! I know this may sound cheeky but i have hardly any experience with vb.net and the link that you provided me with looks like jibberish to me! is there any chance you could work it out and post the code to me. Not to worry if this is too much of an ask.
-
May 27th, 2010, 08:21 AM
#4
Re: Subtracting time values..HELP!
It's not like it's a big deal, this is something that is done all the time. Part of your problem is you posted in the wrong section (I've asked that it be moved, so you don't need to do anything).
The other half of the problem is that you've set yourself up for failure right out of the box by storing your times in a TextBox (there's nothing wrong with displaying it as such, but storing it as a string is the first ingredient in the recipe for disaster.)
Code:
Private startTime As DateTime
Private stopTime As DateTime
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.BackColor = Color.Red
Button1.Text = "In Use"
startTime = TimeOfDay
TextBox1.Text = startTime
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Button1.BackColor = Color.ForestGreen
Button1.Text = "Table 1"
stopTime = TimeOfDay
TextBox2.Text = stopTime
Dim elapsedTime As TimeSpan = stopTime.Subtract(startTime)
'MessageBox.Show("Hours in use: " & elapsedTime.Hours.ToString)
'MessageBox.Show("Total Minutes in use: " & elapsedTime.TotalMinutes.ToString)
MessageBox.Show("Time in use: " & elapsedTime.ToString) '( "hh:MM:ss"))
End Sub
I moved the start and end time variables outside of the event handlers.... then I used the TimeSpan object to get the difference between the two. It provides a .Hours .Minutes and a .Seconds properties that will give you the respective hours minutes and second in the time span.... there's also a TotalHours TotalMinutes and TotalSeconds properties... the difference is that Hours Minutes and Seconds will "reset" ... so when Seconds goes from 59 to 60, it resets to 0, and Minutes is incremented.... While TotalSeconds does not, and keeps on incrementing 60, 61, 62, etc. The TimeSpan also has a .ToString which returns the time in hh:MM:ss format.
-tg
-
May 27th, 2010, 08:48 AM
#5
Thread Starter
New Member
Re: Subtracting time values..HELP!
thanks! that has been a big help although the msg box displayes time in use and the stop time. I need the stop time to be displayed in textbox 2, and the elapsed time displayed in textbox4. The elapsed time is not being displayed anywhere at the moment. Sorry to be a pain but my project is in for tommorrow and im panicing lol thanks!
-
May 27th, 2010, 08:56 AM
#6
Re: Subtracting time values..HELP!
Originally Posted by sdk0121
Thanks for your input! I know this may sound cheeky but i have hardly any experience with vb.net and the link that you provided me with looks like jibberish to me! is there any chance you could work it out and post the code to me. Not to worry if this is too much of an ask.
You didn't specify what language you are using, I only could assume it was .Net. Look at the link again, in the Examples section, there are also tabs to choose C#, C++, F# and JScript.
-
May 27th, 2010, 08:59 AM
#7
Re: Subtracting time values..HELP!
OK... so then add that to the code... you were already doing it, so it should be easy to add... JUST DON'T USE the times in the text box... they should be for display only.
-tg
-
May 27th, 2010, 09:22 AM
#8
Thread Starter
New Member
Re: Subtracting time values..HELP!
Thanks for your help and patience guys! Ive managed to sort that out now however I face one final problem, calculating the cost from the elapsed time. It works out at £3 for every hour, £1.50 for half an hour and so on and so for. If anyone could help with this issue i would be over the moon! thanks!
-
May 27th, 2010, 09:26 AM
#9
Re: Subtracting time values..HELP!
300 pence per hour, divided by 60 comes out as 5p per minute. Total minutes times 5p equals total cost.
-
May 27th, 2010, 09:44 AM
#10
Thread Starter
New Member
Re: Subtracting time values..HELP!
thanks but I need this as code as I do not have a clue how to do it! sorry for any inconvenience!
-
May 27th, 2010, 10:04 AM
#11
Re: Subtracting time values..HELP!
the timespan object gives you the total minutes: .TotalMinutes.... so... .totalminutes * 5 = total charges in pence.
-tg
-
May 27th, 2010, 10:15 AM
#12
Re: Subtracting time values..HELP!
-
May 27th, 2010, 10:18 AM
#13
Thread Starter
New Member
Re: Subtracting time values..HELP!
still had no luck! i dont understand the timespan object functions. do I have to Dim anything?
-
May 27th, 2010, 06:52 PM
#14
Re: Subtracting time values..HELP!
Googling ".Net TimeSpan" will get you plenty of examples, like this one: http://articles.techrepublic.com.com...1-5760752.html
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|