Skip to main content
Inspiring
April 23, 2011
Question

Time Difference Span Over Midnight

  • April 23, 2011
  • 2 replies
  • 1955 views

I am trying to calculate time difference for things that happen before and after midnight...

Example - Call Time of 23:58:22 and an Arrival Time of 00:02:50...

Gets all screwed up with <cfset response = DateDiff("s", calltime,artime)>

I need the final value in seconds...

Can't seem to figure this one out...

Thanx,

Merle

    This topic has been closed for replies.

    2 replies

    Inspiring
    April 23, 2011

    My bad - didn't explain quite as much...

    Calltime and Artime are CFSETS

    <cfset calltime = "23:58:50">

    <cfset artime = "00:02:50">

    This works - if times are same day etc...

    <cfset response = DateDiff("s", calltime,artime)>

    Trouble is the span over midnights of days...

    This doesn't quite work... I need a final result in total seconds...

    #TimeFormat(DateAdd('n', DateDiff('s', calltime, enrtime) ,'00:00'), 'hh:mm')#

    Result [ 02:30 ]  - but I need total seconds... which should be 4 mins = 240 secs

    Thanx

    Inspiring
    April 23, 2011

    Right. And did you read my post?

    --

    Adam

    Inspiring
    April 23, 2011

    Deleted one posting...

    The Adobe Doc isn't too helpfull...

    I am working with some other code that might work...

    Has to be a common problem...

    It is for our fire dept reporting system - to figure out response times...

    Pulling my hair out...

    DateDiff works fine as long as the times line up and don't cross midnight...

    Inspiring
    April 23, 2011

    First things first. This:

    Gets all screwed up with <cfset response = DateDiff("s", calltime,artime)>

    is not a very useful way of describing a problem you're having.  Don't say something is "screwed up", because that could mean anything.  Say "I was expecting x, and I got y".

    Example - Call Time of 23:58:22 and an Arrival Time of 00:02:50...

    Gets all screwed up with <cfset response = DateDiff("s", calltime,artime)>

    Also, don't describe your code, post your code.  I presume you have two CFSET statements along these lines, somewhere:

    <cfset calltime = "23:58:33">

    <cfset artime = "00:02:50">

    But I can only assume that, because you haven't actually told us.

    If this is the case, you need to read the docs slightly more carefully:

    http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-6dd1.html

    Note that the second and third arguments are date objects, not strings.  "23:58:33" is a string.  If you want a function to work properly, you need to pass it the correct arguments.  In this case you need to tell CF that the second time you are passing is the following day from the first one.  Just because you know it is, doesn't mean CF has any way of knowing it.  So pass a proper date/time (created via createDateTime() or something like that) to the function.

    --

    Adam