Time Elapsed

Lanc1988

Contributor
Joined
Nov 27, 2003
Messages
508
I have a countdown timer on my form that counts down from 20:00 minutes.. but I would also like to add another timer that counts the time elapsed (the time the form has been open, ill call that playing time)

I want it so it updates the label each second with how long the form has been open.

so 00:01, then 00:02, etc..

Heres the code i used for the countdown timer.. maybe someone knows how i can reverse it to count up instead.
Visual Basic:
    Private myStartTime As DateTime

        'Sets timer to countdown when enabled
        Dim myTimeElapsed As TimeSpan = DateTime.Now.Subtract(myStartTime)
        Dim myTimeLeft As New TimeSpan(0, 20, 1)
        myTimeLeft = myTimeLeft.Subtract(myTimeElapsed)
        lblTime.Text = myTimeLeft.Minutes.ToString("00") & ":" & myTimeLeft.Seconds.ToString("00")
 
ok, heres what i tried, and im getting one error:
Visual Basic:
    Private myPlayingTime As DateTime

    Private Sub TimePlaying_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimePlaying.Tick

        Dim myTimePlayed As TimeSpan = DateTime.Now.Add(myPlayingTime)
        Dim myTotalTime As New TimeSpan(0, 0, 0)
        myTotalTime = myTotalTime.Add(myTimePlayed)
        lblTimePlaying.Text = myTotalTime.Minutes.ToString("00") & ":" & myTotalTime.Seconds.ToString("00")

    End Sub

Error is on "Dim myTimePlayed As TimeSpan = DateTime.Now.Add(myPlayingTime)"

It says in the tooltip: Value of type 'Date' cannot be converted to 'System.TimeSpan'.
 
Maybe you should be subtracting myPlaying time from Now in order to see how long your form has been open.
DateTime.Now.Subtract(myPlayingTime)
'This should return a timespan object when you subtract two date values.

Date + TimeSpan = Date,
Date - Date = TimeSpan
Date - TimeSpan = Date
:)

I wonder why you are adding a timespan to an empty timespan, though.
 
ok, now that i replaced the first add with subtract it now counts up, but i still have one problem.. its starting at whatever time the computer's clock is at.. like its 4:34 on my computer so it starts at 34:00

but otherwise its working fine..
 
Last edited:
anyone? I'm sure I just have one small part of the code wrong.. I need it to start out with 00:00 instead of whatever the system's time is, which defeats the purpose of having a time form has been open.
 
so would this work?:


Load Event:
Visual Basic:
Timer1.Enabled = True
Dim PlayingTimeValue As String
PlayingTimeValue = DateTime

myPlayingTime = Date.Time - Date.Time
Visual Basic:
    Private myPlayingTime As DateTime
Timer Event:
Visual Basic:
    Private Sub TimePlaying_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimePlaying.Tick

        Dim myTimePlayed As TimeSpan = DateTime.Now.Add(myPlayingTime)
        Dim myTotalTime As New TimeSpan(0, 0, 0)
        myTotalTime = myTotalTime.Add(myTimePlayed)
        lblTimePlaying.Text = myTotalTime.Minutes.ToString("00") & ":" & myTotalTime.Seconds.ToString("00")

    End Sub
 
myPlayingTime = Date.Time - Date.Time
What is this supposed to do?
Subtracting something from itself will give you 0.

You should store DateTime.Now into a datetime variable at the start of your program.

Then, in the timer, you will subtract your current value from DateTime.Now.
DateTime.Now will be greater than the value that you have in the datetime variable, so you do: DateTime.Now - dtvar to get a timespan that has the difference of time. :)
 
im so confused on what even to try... would it be possible for you to add that line to my code so it will work right.. I have been thinking of how to do this for days now.. :S
 
It would probably be better for you to clean everything out and start from scratch, so that you can clear your mind.

Declare a DateTime variable: Dim D As DateTime
In the Load Event, you set D to the current time.
D = DateTime.Now

In the timer event you declare a timespan
Dim TS As Timespan
And then subtract D from DateTime.Now and put the result into timespan.
TS = D - DateTime.Now
Then, display the properties of the timespan anyway you want. (Your old way is fine, probably) :)
 
ok, thanks alot, works very nice now.. I have just one question.. Is there a way to put the 'Dim D As DateTime' in like a button so its just above the line that enables the timer?

I think that it would need to be a form wide variable somehow.. im not sure how to do something like that, cuz if I just move the Dim D As DateTime to the line just above the Timer1.Enabled = True then in the TimerTick event it has an error cuz the "D" isn't declared
 
So, you don't know how to declare Dim D As DateTime as a form-wide variable?

You need to Dim D As DateTime outside of all of the subs.
So, find the first Private Sub in your form class, and put Dim D As DateTime on the lin before that. :)
 
I have tried that before.. but the problem is, right when the form opens it is stores the time.. making my time player timer incorrect.. so I need it as a form-wide variable, yet it has to store the time like right as the timer is enabled.
 
The Declaration shouldn't be storing the time. That comes from the line
D = Now

So, you need to move that line to the line immediately after you enable the timer. :)
This puts the time when the timer was enabled into the date variable.
Clearly, you couldn't have done this in the Timer event, as the Timer would reset this variable quite a few times.
 
ok, the problem still is that when i delcare to store the time, it is under a sub for a menuitem.. so.. that means in the TimerTick events that it thinks "D" is not declared..
 
I like to declare my form level variables right after the "Windows Form Designer Generated Code statement."

Just declare any form level variables there the same as in a sub procedure. Such as:

Dim D As DateTime

Now any sub will have access to this variable. In a button click event, you could say something like:

D = DateTime.Now

Now this value will be in the form level variable, until it is either changed or the form is reloaded. Again any sub procedure will have access to this variable so you can do whatever you want to with it.

Chester
 
Back
Top