Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Time Difference Span Over Midnight

Participant ,
Apr 22, 2011 Apr 22, 2011

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

1.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 23, 2011 Apr 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 23, 2011 Apr 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 23, 2011 Apr 23, 2011

Right. And did you read my post?

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 23, 2011 Apr 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...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 23, 2011 Apr 23, 2011

Looks like I found a woraround solution that seems to be working...

<cfset ensec=DateDiff("s",calltime,enrtime)>
<cfset days=int(ensec/86400)>
<cfset hours=int((ensec-(days*86400))/3600)>
<cfset minutes=int((ensec-(days*86400)-(hours*3600))/60)>
<cfset seconds=(ensec-(days*86400)-(hours*3600)-(minutes*60))>

Enr Time difference is #days# days, #hours#:#minutes#:#seconds#
Total Enr
<cfset tenr=((minutes*60)+seconds)> #tenr# seconds

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 23, 2011 Apr 23, 2011

Did you actually read my reply?

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 23, 2011 Apr 23, 2011

Read lots of stuff and found a solution

The adobe help file was not too much help

Sent from Merle the Pearls iPhone

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 23, 2011 Apr 23, 2011

Yes you did find a very hammer to insert a screw solution.

I would just have done something along these lines:

<cfset oneDateTime = createDateTime(2011,04,23,21,35,26)>

<cfset twoDateTime = createDateTime(2011,04,24,00,15,13)>

<cfoutput>#dateDiff("s",oneDateTime,twoDateTime)#</cfoutput>

But, if you prefer your much more complex solution to avoid adding DATES to your DATE-TIME data so that Coldfusion's functions can understand that the two times are on two different DAYS then enjoy.

Which is all Adam and the documentation was trying to tell you to do.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 23, 2011 Apr 23, 2011

Wow...

Didn't expect to get flak about this...

The Adobe article was a bit of bunk without - looking for solutions anyways...

Hence read this...

Yet again, this example is beset with irrelevant stuff. We dont need a form example.
We just want to know how to use the function.

One problem I had was - no date was associated with the time insert..

This reason is because I was trying to prevent user errors...

Which are the hardest... - but because of CF8 time insert stuff - it will accept invalid times even when prompted with CFFORM validation requirements...

An example is - a user could enter a time such as - 01:02:33 and be fine - but could also enter 01:02:66 and the CFFORM validation would take the 66 seconds... So - this led me to a user error if they enter it wrong because it causes a database error on the insert...

Which then led me to creating the appropriate times by drop down to force the user to not enter weird times...

Solved that problem... But - I then had to reconstruct the time using the 3 drop downs... ( HH:MM:SS - each with a drop down)

I didn't really need the date so it wasn't passed... As it would then have to be added and constructed to each of the 6 response times needed...

Plus - I would now need to go back and update database insert stuff on old records...

So fix was found as I posted... Yes - not clean - but works...

It is not a huge resource dependant site that would cause any problems on the server...

I'll take suggestions for improving fixes in the future...

Thanx for any help - it all helps us all one way or the other...

Merle

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 23, 2011 Apr 23, 2011

I'm going to try elaborate further and add some coding for any help to others...

First things first... CFFORM

Time Vaildation Errors on the seconds...

<cfinput type="Text" name="WhateverTime" required="Yes" message="Please Enter Time (HH:MM:SS)" size=6 validate="time">

It will not take 26:11:22 nor will it take 13:76:33 - but it would take 17:55:77 (obviously 77 is no good)

This will accept the entry but error on Database Insertion/Validation...

Here is code to force appropriate time entry - to avoid the user error - data entry problem...

It forces only good numbers...

---------------------------------------------

<cfselect name="aiqtimeh" class=verd8>
   <option value="00">00</option>
   <option value="01">01</option>
   <option value="02">02</option>
   <option value="03">03</option>
   <option value="04">04</option>
   <option value="05">05</option>
   <option value="06">06</option>
   <option value="07">07</option>
   <option value="08">08</option>
   <option value="09">09</option>
   <option value="10">10</option>
   <option value="11">11</option>
   <option value="12">12</option>
   <option value="13">13</option>
   <option value="14">14</option>
   <option value="15">15</option>
   <option value="16">16</option>
   <option value="17">17</option>
   <option value="18">18</option>
   <option value="19">19</option>
   <option value="20">20</option>
   <option value="21">21</option>
   <option value="22">22</option>
   <option value="23">23</option>
</cfselect> :
<cfselect name="aiqtimem" class=verd8>
   <option value="00">00</option>
   <option value="01">01</option>
   <option value="02">02</option>
   <option value="03">03</option>
   <option value="04">04</option>
   <option value="05">05</option>
   <option value="06">06</option>
   <option value="07">07</option>
   <option value="08">08</option>
   <option value="09">09</option>
   <option value="10">10</option>
   <option value="11">11</option>
   <option value="12">12</option>
   <option value="13">13</option>
   <option value="14">14</option>
   <option value="15">15</option>
   <option value="16">16</option>
   <option value="17">17</option>
   <option value="18">18</option>
   <option value="19">19</option>
   <option value="20">20</option>
   <option value="21">21</option>
   <option value="22">22</option>
   <option value="23">23</option>
   <option value="24">24</option>
   <option value="25">25</option>
   <option value="26">26</option>
   <option value="27">27</option>
   <option value="28">28</option>
   <option value="29">29</option>
   <option value="30">30</option>
   <option value="31">31</option>
   <option value="32">32</option>
   <option value="33">33</option>
   <option value="34">34</option>
   <option value="35">35</option>
   <option value="36">36</option>
   <option value="37">37</option>
   <option value="38">38</option>
   <option value="39">39</option>
   <option value="40">40</option>
   <option value="41">41</option>
   <option value="42">42</option>
   <option value="43">43</option>
   <option value="44">44</option>
   <option value="45">45</option>
   <option value="46">46</option>
   <option value="47">47</option>
   <option value="48">48</option>
   <option value="49">49</option>
   <option value="50">50</option>
   <option value="51">51</option>
   <option value="52">52</option>
   <option value="53">53</option>
   <option value="54">54</option>
   <option value="55">55</option>
   <option value="56">56</option>
   <option value="57">57</option>
   <option value="58">58</option>
   <option value="59">59</option>  
</cfselect> :
<cfselect name="aiqtimes" class=verd8>
   <option value="00">00</option>
   <option value="01">01</option>
   <option value="02">02</option>
   <option value="03">03</option>
   <option value="04">04</option>
   <option value="05">05</option>
   <option value="06">06</option>
   <option value="07">07</option>
   <option value="08">08</option>
   <option value="09">09</option>
   <option value="10">10</option>
   <option value="11">11</option>
   <option value="12">12</option>
   <option value="13">13</option>
   <option value="14">14</option>
   <option value="15">15</option>
   <option value="16">16</option>
   <option value="17">17</option>
   <option value="18">18</option>
   <option value="19">19</option>
   <option value="20">20</option>
   <option value="21">21</option>
   <option value="22">22</option>
   <option value="23">23</option>
   <option value="24">24</option>
   <option value="25">25</option>
   <option value="26">26</option>
   <option value="27">27</option>
   <option value="28">28</option>
   <option value="29">29</option>
   <option value="30">30</option>
   <option value="31">31</option>
   <option value="32">32</option>
   <option value="33">33</option>
   <option value="34">34</option>
   <option value="35">35</option>
   <option value="36">36</option>
   <option value="37">37</option>
   <option value="38">38</option>
   <option value="39">39</option>
   <option value="40">40</option>
   <option value="41">41</option>
   <option value="42">42</option>
   <option value="43">43</option>
   <option value="44">44</option>
   <option value="45">45</option>
   <option value="46">46</option>
   <option value="47">47</option>
   <option value="48">48</option>
   <option value="49">49</option>
   <option value="50">50</option>
   <option value="51">51</option>
   <option value="52">52</option>
   <option value="53">53</option>
   <option value="54">54</option>
   <option value="55">55</option>
   <option value="56">56</option>
   <option value="57">57</option>
   <option value="58">58</option>
   <option value="59">59</option>  
</cfselect>

---------------------------------------------------------

So here lies the orignal problem...

Data entry for the report is by hand...

So it is not done automatically...

A CreateTime Now() will not work for any entered time...

As it might be a report from a week ago... Or as related spans across midnight...

With this reporting system - there is no real chance of moving out further than 1 day - an incident is mitigated without spanning several days...

If times are all lined up properly - and not spanning over midnight - no problem...

ie:

<cfif calltime LTE ConcTime>
<cfset callduration = DateDiff("s", calltime,conctime)> - (forgive the different time notes calltime / enrtime - there are several that are being tracked/used)

</cfif>
<cfif calltime GTE ConcTime>
<cfset sec=DateDiff("s",calltime,conctime)>
<cfset days=int(sec/86400)>
<cfset hours=int((sec-(days*86400))/3600)>
<cfset minutes=int((sec-(days*86400)-(hours*3600))/60)>
<cfset seconds=(sec-(days*86400)-(hours*3600)-(minutes*60))>
<cfset callduration=((hours*60*60)+(minutes*60)+seconds)>
</cfif>

If Call time is Less ThanConclusion Time - Works fine... Dandy - if on same day...

If not - GTE code is invoked...

Took someone to figure out the math etc...

From the Forum - if u are passing the dates...

<cfset oneDateTime = createDateTime(2011,04,23,09,35,26)>

<cfset twoDateTime = createDateTime(2011,04,24,12,15,13)>

<cfoutput>#dateDiff("s",oneDateTime,twoDateTime)#</cfoutput>

I'm not passing the dates - and would have to create the same

<cfif calltime LTE ConcTime> etc...
or

<cfif calltime GTE ConcTime> etc...

To start manipulating the date functions...

So it is half a dozen of one or another...

Hopefully this helps anyone with a similar problem...

Merle

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 23, 2011 Apr 23, 2011

Above solution should have a few acceptable answers.

Thanx all...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 24, 2011 Apr 24, 2011

I think it would have been simpler to base your dates on the times.  If time2 is greater than time1, the dates are equal.  Otherwise date1 is the day before date2.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 30, 2011 Apr 30, 2011
LATEST

First things first... the fact that CF8 has bugs with its time validation is perhaps a perfect reason to suggest that you're not compelled to use <CFINPUT> and <CFSELECT> when you're creating a form.  You can just use vanilla HTML controls (and roll your own validation that actually works...).  You're gaining very little from using <CFINPUT> here, and nothing at all from using <CFSELECT>.  I also sure hope you're not really hard-coding all those <OPTION> tags manually.  There are looping constructs for that sort of thing.

Anyway... so you seem to associate a date to the callout itself, but not to each individual time stamps.

However one can easily infer that the end timestamp is the day following the start time stamp:

<cfif "#form.startTimeHH##form.startTimeMM##form.startTimeSS#" LT "#form.endTimeHH##form.endTimeMM##form.endTimeSS#">

     <!--- it's like 12:12:12 and 15:15;15, so same day --->

<cfelse>

     <!--- it's like 15:15:15 and 12:12:12, so the end time must be the following day

</cfif>

So from there you have your callout start date (which I presume they enter via some other form control), and can infer whether the date part of the endTime is the same date or the following date.

This only works if the dateDiff between the start and finish time is only within a 24h period though.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 24, 2011 Apr 24, 2011

Yes you did find a very hammer to insert a screw solution.

>

Perhaps uncharacteristically of me, I resisted the urge to make a reply like

that. Hoping you or Owain might come along with a similar comment.

I would just have done something along these lines:

>

<cfset oneDateTime = createDateTime(2011,04,23,09,35,26)>

<cfset twoDateTime = createDateTime(2011,04,24,12,15,13)>

<cfoutput>#dateDiff("s",oneDateTime,twoDateTime)#</cfoutput>

>

>

But, if you prefer your much more complex solution to avoid adding DATES to

your DATE-TIME data so that Coldfusion's functions can understand that the

two times are on two different DAYS then enjoy.

>

The thing is, to know the latter time was "the following day" (s)he must

already have the date information there available.

Which is all Adam and the documentation was trying to tell you to do.

>

Quite.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 24, 2011 Apr 24, 2011

Adam Cameron. wrote:

Perhaps uncharacteristically of me, I resisted the urge to make a reply like

that.  Hoping you or Owain might come along with a similar comment.

I tried to contribute to the thread earlier in the day yesterday.  But at that time, my computer and|or Jive's software were not working proplerly.  So I could not get to the thread to post anything.

Then I went and enjoyed my Saturday.  When I got back online at the end of the day, the thread had gotten to it's conclusion.  While phrased to answer the orginal post, I suspect there is a good chance s|he will not bother comming back.  But other people might come along looking for a solution to a simlar problem and they should get a better answer.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 23, 2011 Apr 23, 2011

Coldfusion has a createdatetime function.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources