Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Time Compare: show the elapsed time 1

Status
Not open for further replies.

Bramvg

IS-IT--Management
Jan 16, 2001
135
0
0
BE
Hi,

I'm trying to find a find to compare two 'times'. I tried to use datecompare, but as the function indicates it's only to compare two dates.

I would like to know the difference betweed an start and an end time, including a day. Because someone can access my website at midnight and continue 'the day after'.
E.g.: starting at monday on 23.59 and stop on tuesday 01.00u

E.g:

Starttime: 11:15:20 [HH:MM:SS]
Endtime: 12:16:21 [HH:MM:SS]

Should I also include the day?

The result should be:
01:01:01 [HH:MM:SS]

Is theire a way of doing this?

Many thanks in advance.
Bram


 
First of all you will always have to add the date, because if the time is around midnight you cannot compare the times. Furthermore I would use the database functions to compare the dates or calculate the time elapsed.

For instance in SQL Server you can subtract two datetime values. And with that you get the time elapsed.

JNC73
 
Hi,

Following is a coldfusion code that roughly calculates the time in second used by a stored procedure. If you want the formate in your post, you can further convert the time in second to hour, minute, and second.


<cfset smpstart=((hour(now()))*3600 + (minute(now()))*60 + second(now()))>
<cfstoredproc procedure=&quot;psamplecustpvc2&quot; datasource=&quot;icore&quot;>
<cfprocparam type=&quot;in&quot; cfsqltype=cf_sql_date value='#form.fromdate#'>
<cfprocparam type=&quot;in&quot; cfsqltype=cf_sql_date value='#form.todate#'>
<cfprocparam type=&quot;in&quot; cfsqltype=cf_sql_integer value='#lcustid#'>
<cfprocparam type=&quot;in&quot; cfsqltype=cf_sql_integer value='#form.domid#'>
<!--- <cfprocparam type=&quot;in&quot; cfsqltype=cf_sql_integer value='#samplesize#'> --->
<cfprocresult name=&quot;samplecustpvc&quot;>
</cfstoredproc>
<cfset smpend=((hour(now()))*3600 + (minute(now()))*60 + second(now()))>
<cfset smptimediff = (smpend - smpstart)>

Good Luck

Xiaoming
 
Hey Bram,

Does this do what you want?

<cfset sDate=&quot;03/01/2001 11:30:15 am&quot;>
<cfset eDate=&quot;04/01/2001 01:30:15 pm&quot;>

<cfset sec=DateDiff(&quot;s&quot;,sDate,eDate)>
<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))>

<cfoutput>Time difference is #days# days, #hours#:#minutes#:#seconds#
</cfoutput>

GJ
 
Hi GunJack

It does E X A C T L Y what it has to do!

Many thanks, again ;-)

bram
 
Just for fun, here is another algorithm:

<cfset oneday = 86400>
<cfset onehour = 3600>
<cfset onemin = 60>

<cfset days = int(sec/oneday)>
<cfset remainder = sec MOD oneday>
<cfset hours = int(remainder/onehour)>
<cfset remainder = remainder MOD onehour>
<cfset minutes = int(remainder/onemin)>
<cfset seconds = remainder MOD onemin>

<cfoutput>Time difference is #days# days,
#hours#:#minutes#:#seconds# sec
</cfoutput>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top