DateDiff function returning incorrect results in asp.net 2.0 vb

G

Guest

I am trying to return the difference in minutes from a starttime and stoptime
using the datediff function in a Web Project with VB.Net

With:
StartTime = 2/10/2006 8:46:03 PM
EndTime = 2/10/2006 8:48:10 PM

I am getting a return value of 1054586688 in the diff variable

I tryed this same code in a Windows.Net Project and I get a return of 2 as I
would expect.

Can someone tell me what I need to do in a Web Project to make this simple
code work?


Code from Web Project
==================

Partial Class _Default
Inherits System.Web.UI.Page

Dim StartTime As Date

Protected Sub btnStartTime_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnStartTime.Click
StartTime = Now
lblStartTime.Text = StartTime
End Sub

Protected Sub btnStopTime_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnStopTime.Click
Dim EndTime As Date = Now
lblStopTime.Text = EndTime
Dim Diff As Integer = DateDiff(DateInterval.Minute, StartTime,
EndTime)
txtTime.Text = Diff

End Sub
End Class
 
C

Cor Ligthert [MVP]

Reney,

This code will in my idea never works, you have to do with needed time for
sending and getting your page from the server. This kinds of actions on a
webclient need JavaScript, in your place would I search for JavaScript
StopWatch on Google.

I hope this helps,

Cor
 
G

Guest

Hi Cor,

What I am trying to do is create a timeslip to time work on a issue. The
tech can start the clock and stop the clock. I also want to have a text box
displaying the differences between the two time that can be edited.

I believe you when you say it can't be done with the code I displayed in my
post (I know that :)

What I need is a code example on how I can make this work and I am also
curious as to why this code works in a windows application but not in a Web
project.

If I could ask you to expand your answer that would be most helpful. And
yes I am new to Web development (sorry I am DBA that got tossed into
developement)
 
G

Guest

Ok Cor you can do this with vb.net - what I was missing was session.

here is the code that works in vb

Partial Class Default2
Inherits System.Web.UI.Page
Dim StartTime As Date

Protected Sub btnStartTime_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnStartTime.Click
Session("StartTime") = Date.Now
lblStartTime.Text = Session("StartTime")
End Sub

Protected Sub btnStopTime_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnStopTime.Click
Dim EndTime As Date
Session("EndTime") = Date.Now
Dim Diff As TimeSpan
Diff = Session("EndTime") - Session("StartTime")
txtTime.Text = Diff.Hours & ":" & Diff.Minutes & ":" & Diff.Seconds
lblStopTime.Text = Session("EndTime")
End Sub
End Class
 
C

Cor Ligthert [MVP]

Reney,

The result is not interesting because what you calculate is in my opinion
without sense.

The user clicks the button start time
The page is sent back to the server
The start time is set
The page is sent back to the client
The user clicks the button stop time
The page is sent back to the server
The time elapsed is calculated
The page is sent back to the client.
The user sees an elapsed time

What time did you measure?

Cor
 
G

Guest

Gee wiz Cor you must be having a bad day! The measure is the difference
between the start and end time someone worked on a support ticket for a time
tracking system. I simpified the example I posted to make it easier to fit in
the post.

And the question had pretty good logic behind it - in a windows app the code
works but additional code (wrapping the variable in Session) is needed for a
web application.
 
C

Cor Ligthert [MVP]

Reney,
Gee wiz Cor you must be having a bad day!

You could be right, however beside this text can you than explain how your
solution can work?

Your sample show in my idea that it does not work for webapplication. Do you
have sample that works.=?

Beside that it gives technical a result. However the result is something
what measures something that can never been asked because even when it would
measure the sending and retrieving time it is incorrect..

Cor
 
C

Cor Ligthert [MVP]

Reney,

By the way, you should not test this on your testing computer however on a
real internet or heavily occupied intranet (your testing computer will
probably reach the result you want).

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top