Subtracting time values..HELP!-VBForums
Results 1 to 14 of 14

Thread: Subtracting time values..HELP!

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    10

    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

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    10

    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.

  4. #4
    PowerPoster
    Join Date
    May 2002
    Posts
    25,960

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    10

    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!

  6. #6
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Subtracting time values..HELP!

    Quote Originally Posted by sdk0121 View Post
    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.

  7. #7
    PowerPoster
    Join Date
    May 2002
    Posts
    25,960

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    10

    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!

  9. #9
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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.

  10. #10

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    10

    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!

  11. #11
    PowerPoster
    Join Date
    May 2002
    Posts
    25,960

    Re: Subtracting time values..HELP!

    the timespan object gives you the total minutes: .TotalMinutes.... so... .totalminutes * 5 = total charges in pence.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Subtracting time values..HELP!

    Moved To VB.NET
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  13. #13

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    10

    Re: Subtracting time values..HELP!

    still had no luck! i dont understand the timespan object functions. do I have to Dim anything?

  14. #14
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.