Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27184

VS 2012 Calculating Time

$
0
0
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?

Viewing all articles
Browse latest Browse all 27184

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>