Results 1 to 2 of 2

Thread: [RESOLVED] Calculating Time

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    51

    Resolved [RESOLVED] Calculating Time

    I know there's a boat load of examples of this out there, but I've confused the hell out of myself.

    I have 4 timers that I run on a form from a button click. They can also get started from Form_Load if certain conditions are met. Posting just one Timer block because only the variable names and label that I display the time in change in the other 3.

    Code:
        'Private Sub SoldiersTimer_Tick(sender As Object, e As EventArgs) Handles SoldiersTimer.Tick
        '    soldierstime = soldierstime - 1
        '    soldierts = TimeSpan.FromSeconds(soldierstime)
        '    SoldiersTimeLabel.Text = String.Format("{0:00}:{1:00}:{2:00}", soldierts.Hours, soldierts.Minutes, soldierts.Seconds)
        '    If SoldiersTimeLabel.Text = "00:00:00" Then
        '        SoldiersTimer.Stop()
        '    call update procedure to do some things for all accounts
        '    End If
        'End Sub
    Code:
    'Declarations
    Dim bfdateloggedoff as Date
    Dim difference, soldierts, guardsments, specialforcests, thievests as TimeSpan
    Dim soldierstime, guardsmentime, specialforcestime, thievestime As Integer
    My Form_Load

    Code:
    'get the date and time the user last logged out from the db
    bfdateloggedoff = dsaccounts.Tables("Accounts").Rows(reccount).Item(11) '(default value in db until I get things rolling 1/1/2013 12:00:01 AM)
    
    'calculate the time difference between Now() and the time the user logged out
    
    difference = bfdateloggedoff.Subtract(Now())
    
    'timer time in seconds to countdown = number of units * 60 seconds * total minutes it will take for 1 unit
    'we need to know the beginning number the user started with before they logged out
    
    soldierstime = dsmilitary.Tables("Military").Rows(reccount).Item(4) * 60 * 12
    guardsmentime = dsmilitary.Tables("Military").Rows(reccount).Item(6) * 60 * 12
    specialforcestime = dsmilitary.Tables("Military").Rows(reccount).Item(8) * 60 * 12
    thievestime = dsmilitary.Tables("Military").Rows(reccount).Item(10) * 60 * 12

    Here is where I am stuck. Trying to translate this from english to code:

    I need to compare the value in my 4 variables (soldierstime, guardsmentime, specialforcestime, thievestime) to difference.TotalSeconds (the number of seconds elapsed since user logged off to Now())

    Code:
    'continue where we would have left off if we hadn't closed the program
    If variable_name > difference.TotalSeconds Then
       variable_name = variable_name - difference.TotalSeconds
       'Start appropriate timer
    End If
    
    'if time expired while we were logged out, change what we need to change
    If difference.TotalSeconds - variable_name <= 0 Then
       'do database stuff
    End If
    I dont think this is correct because on the first If difference.TotalSeconds returns a negative number based on the two dates I set, so my variable will always be bigger. I believe the second If is correct.

    So maybe I need to just rewrite difference = bfdateloggedoff.Subtract(Now()) and swap the Now() with the date to fix this?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Calculating Time

    If you want a positive result when performing a subtraction then you must subtract the lesser from the greater. If you're talking date/time then the current value is always going to be greater than a value from the past so yes, you should reverse your operands. Also, there's no need to use the Subtract method because DateTime and TimeSpan support mathematical operators nowadays:
    Code:
    difference = Date.Now - bfdateloggedoff
    If you don;t know which is the greater and which is the lesser for some reason then you can always just use Math.Abs on the result to ensure that you have a positive value.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width