Working with time (dateDiff)

Discussion in 'ASP General' started by John Smith, Jan 16, 2005.

  1. John Smith

    John Smith Guest

    Hi,

    I'm having trouble working out the best way of calculating the time
    difference between two times on the same day.
    The example I have found does return the hours (in this case 8) but forgets
    the minutes.

    I am looking to return the hour & the minute but i've had a look at the
    dateDiff format and this doesn't seem possible ?

    Thanks

    J.


    ---------------------------------
    time1 = "09:15"
    time2 = "17:30"


    hours = datediff("h",time1,time2) ' in hours

    response.write(hours)
    ----------------------------------
     
    John Smith, Jan 16, 2005
    #1
    1. Advertising

  2. John Smith

    Rob Meade Guest

    "John Smith" wrote ...

    > I am looking to return the hour & the minute but i've had a look at the
    > dateDiff format and this doesn't seem possible ?


    > hours = datediff("h",time1,time2) ' in hours


    hours = datediff("m", time1, time2) ' in minutes

    hours = hours / 60

    its rough...

    Rob
     
    Rob Meade, Jan 16, 2005
    #2
    1. Advertising

  3. John Smith

    Rob Meade Guest

    "John Smith" wrote ...

    > ---------------------------------
    > time1 = "09:15"
    > time2 = "17:30"
    >
    >
    > hours = datediff("h",time1,time2) ' in hours
    >
    > response.write(hours)
    > ----------------------------------


    So - in the above - hours = 8 and the minutes are lost...

    hours = datediff("n", time1, time2) ' in minutes
    ' hours would now =
     
    Rob Meade, Jan 16, 2005
    #3
  4. John Smith

    Rob Meade Guest

    "John Smith" wrote ...

    > ---------------------------------
    > time1 = "09:15"
    > time2 = "17:30"
    >
    >
    > hours = datediff("h",time1,time2) ' in hours
    >
    > response.write(hours)
    > ----------------------------------


    Hi John,

    Please excuse my first post which I placed an m for minutes instead of the
    n - please then excuse my second post which I sent to early prior to
    finishing a code example...

    Try this!

    <%
    time1 = "09:15"
    time2 = "17:30"

    ' minutes = datediff("n",time1,time2) ' in minutes
    ' response.write(hours)

    hour1 = hour(time1)
    hour2 = hour(time2)

    minutes1 = "00:" & minute(time1)
    minutes2 = "00:" & minute(time2)

    hoursDiff = datediff("h", time1, time2)
    minutesDiff = datediff("n", minutes1, minutes2)

    If minutesDiff > -1 Then

    ' positive hour difference
    timeDiff = hoursDiff & "hrs " & minutesDiff & "mins"

    Else
    ' negative hour difference
    hoursDiff = hoursDiff - 1
    minutesDiff = 60 + minutesDiff

    timeDiff = hoursDiff & "hrs " & minutesDiff & "mins"

    End If

    Response.Write timeDiff
    %>

    Give the above ago - then try changing it so that the minutes of time2 are
    less than the minutes of time1 - still works nicely - you'll need to play
    with different times to test it fully I'm sure there's still an opportunity
    here for error in the above.

    Hope this helps.

    Regards

    Rob
     
    Rob Meade, Jan 16, 2005
    #4
  5. John Smith

    McKirahan Guest

    "John Smith" <> wrote in message
    news:cscc2j$624$...
    > Hi,
    >
    > I'm having trouble working out the best way of calculating the time
    > difference between two times on the same day.
    > The example I have found does return the hours (in this case 8) but

    forgets
    > the minutes.
    >
    > I am looking to return the hour & the minute but i've had a look at the
    > dateDiff format and this doesn't seem possible ?
    >
    > Thanks
    >
    > J.
    >
    >
    > ---------------------------------
    > time1 = "09:15"
    > time2 = "17:30"
    >
    >
    > hours = datediff("h",time1,time2) ' in hours
    >
    > response.write(hours)
    > ----------------------------------


    Const time1 = "09:15"
    Const time2 = "17:30"
    Dim hours, mins
    hours = datediff("h",time1,time2)
    mins = datediff("n",time1,time2)
    If mins > 60 Then mins = mins - hours * 60
    response.write hours & ":" & mins
     
    McKirahan, Jan 16, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Fox

    DateDiff in C#

    Mark Fox, Jul 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    12,947
    Kevin Spencer
    Jul 16, 2003
  2. DateDiff

    , Dec 29, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    478
    Jim in Arizona
    Dec 29, 2006
  3. CJM
    Replies:
    4
    Views:
    224
  4. Tim::.

    Datediff Problem

    Tim::., May 3, 2004, in forum: ASP General
    Replies:
    1
    Views:
    139
    Maarten
    May 3, 2004
  5. Drago

    Datediff good question

    Drago, Jun 5, 2004, in forum: ASP General
    Replies:
    7
    Views:
    140
    Drago
    Jun 8, 2004
Loading...

Share This Page