Solved

Subtracting one time from another

Posted on 2000-04-07
2
193 Views
Last Modified: 2013-12-24
Hello folks,

I am working on my thesis project for University and I am having some trouble with a particular CF task. Any help on this would be greatly appreciated.

Basically what I need to do is record the time that a user has spent on a page. The way I am approaching this is when the user enters the page I have CF record the current time in Microsoft Access, in a field called “TimeIn” and when they exit the page they go to a new page that records the time in another field called “TimeOut”

Then when I output the data,  I would like to subtract the TimeIn from the TimeOut and display the Duration as a separate number.

I am not sure on the best way to achieve this, I have tried many ways but I cannot get it to work.

I would like the desired output to look like this

Time In:  16:24:15
Time Out: 16:25:20
Duration: 1:05

Some other questions pertaining to this problem are:

How should I store the time in the Access database? Should I apply time formatting/mask to the date first or should I do this only when I output the data?

What should I set the Datatype in Access to? Should I set it to Date/Time or as a Text ?

Thank you for any help on this and if someone knows of a better way to achieve this result please let me know how to do it.

Jacques Choquette
0
Comment
Question by:jacqueschoquette
2 Comments
 
LVL 2

Expert Comment

by:paulkd
ID: 2694665
I assume that you will be using Application.cfm to track the time/in and time/out from page to page...

You shold use a datetime field and populate your DB using now(), then when you need to produce a report, you can use the built-in CF date/time functions. You example only shows times - you should use date and time.
0
 
LVL 19

Accepted Solution

by:
cheekycj earned 100 total points
ID: 2694773
DateDiff
Returns the number of intervals in whole units of type Datepart by which Date1 is less than Date2.

Syntax
DateDiff(datepart, date1, date2)

datepart
One of the following strings:

yyyy -- Year
q -- Quarter
m -- Month
y -- Day of year
d -- Day
w -- Weekday
ww -- Week
h -- Hour
n -- Minute
s -- Second
date1
Date/time object in the period from 100 AD to 9999 AD.

date2
Date/time object in the period from 100 AD to 9999 AD.

Usage
If you want to know the number of days between date1 and date2, you can use either Day of Year ("y") or Day ("d").

When datepart is Weekday ("w"), DateDiff returns the number of weeks between the two dates. If date1 falls on a Monday, DateDiff counts the number of Mondays until date2. It counts date2 but not date1. If interval is Week ("ww"), however, the DateDiff function returns the number of calendar weeks between the two dates. It counts the number of Sundays between date1 and date2. DateDiff counts date2 if it falls on a Sunday; but it doesn't count date1, even if it does fall on a Sunday.

If Date1 refers to a later point in time than date2, the DateDiff function returns a negative number.

When passing date/time value as a string, make sure it is enclosed in quotes. Otherwise, it is interpreted as a number representation of a date/time object returning undesired results.

Years from 0 to 29 are interpreted as 21st century values. Years 30 to 99 are interpreted as 20th century values.

Examples
<!--- This example shows the use of DateDiff --->
....
<CFIF IsDefined("FORM.date1") and IsDefined("FORM.date2")>
    <CFIF IsDate(FORM.date1) and IsDate(FORM.date2)>
        <P>This example uses DateDiff to determine the difference
        in     <CFSWITCH EXPRESSION=#type#>
            <CFCASE VALUE="yyyy">years</CFCASE>
            <CFCASE VALUE="q">quarters</CFCASE>
            <CFCASE VALUE="m">months</CFCASE>
            <CFCASE VALUE="y">days of year</CFCASE>    
            <CFCASE VALUE="d">days</CFCASE>    
            <CFCASE VALUE="w">weekdays</CFCASE>    
            <CFCASE VALUE="ww">weeks</CFCASE>    
            <CFCASE VALUE="h">hours</CFCASE>    
            <CFCASE VALUE="n">minutes</CFCASE>    
            <CFCASE VALUE="s">seconds</CFCASE>        
            <CFDEFAULTCASE>years</CFDEFAULTCASE></CFSWITCH>
             dateparts between date1 and date2.
        <CFIF DateCompare(FORM.date1, FORM.date2) is not 0>
        <P>The difference is <CFOUTPUT>#Abs(DateDiff
          (type, FORM.date2, FORM.date1))#</CFOUTPUT>
        <CFSWITCH EXPRESSION=#type#>
            <CFCASE VALUE="yyyy">years</CFCASE>
            <CFCASE VALUE="q">quarters</CFCASE>
            <CFCASE VALUE="m">months</CFCASE>
            <CFCASE VALUE="y">days of year</CFCASE>    
            <CFCASE VALUE="d">days</CFCASE>    
            <CFCASE VALUE="w">weekdays</CFCASE>    
            <CFCASE VALUE="ww">weeks</CFCASE>    
            <CFCASE VALUE="h">hours</CFCASE>    
            <CFCASE VALUE="n">minutes</CFCASE>    
            <CFCASE VALUE="s">seconds</CFCASE>        
            <CFDEFAULTCASE>years</CFDEFAULTCASE></CFSWITCH>.
        <CFELSE>
....

DatePart
Returns the specified part of a date as an integer.

See also DateAdd and DateConvert.

Syntax
DatePart(datepart, date)

datepart
One of the following strings:

yyyy -- Year
q -- Quarter
m -- Month
y -- Day of year
d -- Day
w -- Weekday
ww -- Week
h -- Hour
n -- Minute
s -- Second
date
Any date.

Usage
Years from 0 to 29 are interpreted as 21st century values. Years 30 to 99 are interpreted as 20th century values.

When passing a date/time value as a string, make sure it is enclosed in quotes. Otherwise, it is interpreted as a number representation of a date/time object, returning undesired results.

Examples
<!--- This example shows information available from DatePart --->
<HTML>
<HEAD>
<TITLE>
DatePart Example
</TITLE>
</HEAD>

<CFSET todayDate = Now()>
<BODY>
<H3>DatePart Example</H3>

<P>Today's date is <CFOUTPUT>#todayDate#</CFOUTPUT>.

<P>Using datepart, we can extract an integer representing
the various dateparts from that value
<CFOUTPUT>
<UL>
    <LI>year: #DatePart("yyyy", todayDate)#
    <LI>quarter: #DatePart("q", todayDate)#
    <LI>month: #DatePart("m", todayDate)#
    <LI>day of year: #DatePart("y", todayDate)#
    <LI>day: #DatePart("d", todayDate)#
    <LI>weekday: #DatePart("w", todayDate)#
    <LI>week: #DatePart("ww", todayDate)#
    <LI>hour: #DatePart("h", todayDate)#
    <LI>minute: #DatePart("n", todayDate)#
    <LI>second: #DatePart("s", todayDate)#
</UL>    
</CFOUTPUT>    

</BODY>
</HTML>      



0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now