DateDiff function returning incorrect results in asp.net 2.0 vb

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • reney

    #1

    DateDiff function returning incorrect results in asp.net 2.0 vb

    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.P age

    Dim StartTime As Date

    Protected Sub btnStartTime_Cl ick(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles btnStartTime.Cl ick
    StartTime = Now
    lblStartTime.Te xt = StartTime
    End Sub

    Protected Sub btnStopTime_Cli ck(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles btnStopTime.Cli ck
    Dim EndTime As Date = Now
    lblStopTime.Tex t = EndTime
    Dim Diff As Integer = DateDiff(DateIn terval.Minute, StartTime,
    EndTime)
    txtTime.Text = Diff

    End Sub
    End Class
  • Cor Ligthert [MVP]

    #2
    Re: DateDiff function returning incorrect results in asp.net 2.0 vb

    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


    Comment

    • reney

      #3
      Re: DateDiff function returning incorrect results in asp.net 2.0 v

      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)


      Comment

      • reney

        #4
        Re: DateDiff function returning incorrect results in asp.net 2.0 v

        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.P age
        Dim StartTime As Date

        Protected Sub btnStartTime_Cl ick(ByVal sender As Object, ByVal e As
        System.EventArg s) Handles btnStartTime.Cl ick
        Session("StartT ime") = Date.Now
        lblStartTime.Te xt = Session("StartT ime")
        End Sub

        Protected Sub btnStopTime_Cli ck(ByVal sender As Object, ByVal e As
        System.EventArg s) Handles btnStopTime.Cli ck
        Dim EndTime As Date
        Session("EndTim e") = Date.Now
        Dim Diff As TimeSpan
        Diff = Session("EndTim e") - Session("StartT ime")
        txtTime.Text = Diff.Hours & ":" & Diff.Minutes & ":" & Diff.Seconds
        lblStopTime.Tex t = Session("EndTim e")
        End Sub
        End Class

        Comment

        • Cor Ligthert [MVP]

          #5
          Re: DateDiff function returning incorrect results in asp.net 2.0 v

          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


          Comment

          • reney

            #6
            Re: DateDiff function returning incorrect results in asp.net 2.0 v

            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.

            Comment

            • Cor Ligthert [MVP]

              #7
              Re: DateDiff function returning incorrect results in asp.net 2.0 v

              Reney,
              [color=blue]
              > Gee wiz Cor you must be having a bad day![/color]

              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


              Comment

              • Cor Ligthert [MVP]

                #8
                Re: DateDiff function returning incorrect results in asp.net 2.0 v

                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


                Comment

                Working...