problem with DateDiff

No_Hope

Member
Joined
Sep 21, 2006
Messages
6
Programming Experience
Beginner
Hey

people wonder if some 1 can help me maybe this is my code am doing a VB.NET course and assignment it says to build a stop watch so i have but i am having a problem with 1 line this line :-

VB.NET:
ElapsedTime = DateDiff(DateInterval.Second, StartTime, EndTime)

This is my code

VB.NET:
 Dim StartTime As Date
    Dim EndTime As Date
    Dim ElapsedTime As Date
    Private Sub frmStopWatch_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnstart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnstart.Click
        'Establish and print starting time 
        StartTime = Now
        lblstart.Text = Format(StartTime, "hh:mm:ss")
        lblEnd.Text = ""
        lblElapsed.Text = ""
    End Sub

    Private Sub btnEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnd.Click
        'Find the ending time, compute the elapsed time 
        'put both values in lable boxes
        EndTime = Now
        [COLOR="Blue"]ElapsedTime = DateDiff(DateInterval.Second, StartTime, EndTime)[/COLOR]
        lblEnd.Text = Format(EndTime, "hh:mm:ss")
        lblElapsed.Text = Format(ElapsedTime, "0")
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class

can any see a correction and maybe give me a simple solution

i appreciate it a lot thanks
 
You know there exist StopWatch class in .Net? Also you must check the TimeSpan structure. For example Date.Subtract(Date) will give you a TimeSpan).
 
hey

sorry i don't understand

You know there exist StopWatch class in .Net

but thanks for the reply still have not solved the problem sorry i am nto experienced in VB.NET and only just about understa
 
Hi

thanks for the posts sirry i am not lazy ijust have hard of understanding lol the more i search the harder it seems because there is no particular answer as far as i can see by following the link u gave me ( much appreciated ) i would have to restructure my code right ? so is there any 1 who can look at the code giev nabove and notice the error or what function etc i need to call.

It would make my life a lot easier thanks
 
Erm.. I dont think we use DateDiff any more, we simply get 2 dates and substrct them:

EndTime - StartTime


This produces a timespan object that can be queried for how long an amount of time it represents:

(EndTime - StartTime).TotalSeconds
 
ps; we dont use Format or Now either - replacements for those are:


<Date instance>.ToString(<format string>)
DateTime.Now


The latter is a disambiguation from Microsoft.VisualBasic.DateAndTime.Now
 
Back
Top