Wrox Programmer Forums Calculate time (minutes) between two times
 |
 Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
 Welcome to the p2p.wrox.com Forums. You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com

June 7th, 2006, 06:29 AM
 rtr1900 Authorized User Join Date: Nov 2005 Posts: 72 Thanks: 3 Thanked 0 Times in 0 Posts
Calculate time (minutes) between two times

Hi,

I has a starttime and finish time. I need to calculate the minutos between them. Is there a command that does that?

i have:

Start_time= TimeValue(rs("starttime"))
Finish_time= TimeValue(rs("finishtime"))

Any help is welcome

Cheers,

David

June 7th, 2006, 09:38 AM
 woodyz Friend of Wrox Join Date: May 2006 Posts: 643 Thanks: 0 Thanked 0 Times in 0 Posts

The method you are looking for is DateDiff:

response.write DateDiff("n", Start_time, Finish_time)

The first parameter is a string that indicates the interval you are interested in using - it can be seconds, minutes, hours, days, weeks, months, years or a few other options. Using n for this parameter indicates the difference is in minutes.

Woody Z http://www.learntoprogramnow.com

June 7th, 2006, 09:45 AM
 rtr1900 Authorized User Join Date: Nov 2005 Posts: 72 Thanks: 3 Thanked 0 Times in 0 Posts

Thx WoodyZ.

I also found a way to do it. It gives me also the hours. Athought it is much more complicated (taken from a math formule) it might be interesting:
===
Time_start_hour= Hour(time_first)
Time_start_min= Minute(time_first)
Time_finish_hour= Hour(time_last)
Time_finish_min= Minute(time_last)

' Check if stop minutes are more than start minutes
If (Time_finish_min > Time_start_min) then
Time_total_min= Time_finish_min- Time_start_min
Time_total_hour= Time_finish_hour- Time_start_hour
Else
Time_total_min= (Time_finish_min + 60)- Time_start_min
Time_total_hour= (Time_finish_hour-1)- Time_start_hour
End if
====

Maybe this can help other people also.....

Cheerio and THX again

David

December 17th, 2007, 10:54 AM
 may29adil Registered User Join Date: Dec 2007 Posts: 9 Thanks: 0 Thanked 0 Times in 0 Posts

Dear i am facing a probelm to calculate the time between two times in asp i m using this code from your posted topic at forums http://p2p.wrox.com/topic.asp?TOPIC_ID=45372 but dear problem is if a person login at 11/11/07 11:30PM and logout at 12/11/07 01:30AM the person has spent 2hrs but following code shows -23:00 how to correct this problem since it only occurs when there is a change of day

my code is below

Time_In=(rec("Time_In"))

Time_start_hour= Hour(time_in)
Time_start_min= Minute(time_in)
Time_finish_hour= Hour(time)
Time_finish_min= Minute(time)

'***************Time Calcultion ***************************
start_time = Time_start_hour &":"& time_start_min
finish_time = Time_finish_hour &":"& Time_finish_min
'Min_Diff = DateDiff("n", Finish_time, Start_time)
response.write(min_diff)
'finish_time = time
' Check if stop minutes are more than start minutes
If (Time_finish_min > Time_start_min) then
Time_total_min= Time_finish_min- Time_start_min
Time_total_hour= Time_finish_hour- Time_start_hour
Else
Time_total_min= (Time_finish_min + 60)- Time_start_min
Time_total_hour= (Time_finish_hour-1)- Time_start_hour

end if

Thanks

December 17th, 2007, 12:13 PM
 woodyz Friend of Wrox Join Date: May 2006 Posts: 643 Thanks: 0 Thanked 0 Times in 0 Posts

If you work with actual dates (which include time) you can then use the Date functions that are provided by VBScript. Here is an example:

Code:
```<%
Start_Time = now
response.write "Start_Time: " & Start_Time & "<br/>"
response.write "End_Time: " & End_Time & "<br/>"

response.write "Difference in minutes: " & DateDiff("n", Start_Time, End_Time) & "<br/><br/>"

Start_Time = "11/11/07 11:30PM"
End_Time = "11/12/07 1:30AM"
response.write "Start_Time: " & Start_Time & "<br/>"
response.write "End_Time: " & End_Time & "<br/>"
response.write "Difference in hours: " & DateDiff("h", Start_Time, End_Time)
%>```
This will ouput the following
Start_Time: 12/17/2007 8:09:52 AM
End_Time: 12/17/2007 8:14:52 AM
Difference in minutes: 5

Start_Time: 11/11/07 11:30PM
End_Time: 11/12/07 1:30AM
Difference in hours: 2

You can see that the first parameter in DateDiff is used to indicate the time or date unit that you want for the result of the DateDiff call.

Cheers,

Woody Z
http://www.learntoprogramnow.com
How to use a forum to help solve problems

December 17th, 2007, 12:45 PM
 may29adil Registered User Join Date: Dec 2007 Posts: 9 Thanks: 0 Thanked 0 Times in 0 Posts

Thanks for reply but code is still not working

i use this code

start_time = time_in

End_Time = now

response.write "Difference in hours: " & DateDiff("h", Start_Time, End_Time)

but not Calculation is incorrect if you like i may send you my file you may check it and fix this error please

Thanks

December 17th, 2007, 03:50 PM
 woodyz Friend of Wrox Join Date: May 2006 Posts: 643 Thanks: 0 Thanked 0 Times in 0 Posts

Did you try my example? Make sure you can get that to work first. It will help you understand how this works.

Your time_in must contain a date for this to work.
If time_in is not a date, you have to do whatever is needed to turn it into a date.

What value is contained in the time_in variable in your code?

Woody Z
http://www.learntoprogramnow.com
How to use a forum to help solve problems

December 18th, 2007, 12:27 AM
 surendraparashar Authorized User Join Date: Dec 2005 Posts: 28 Thanks: 1 Thanked 0 Times in 0 Posts

For your purpose the following method is usefull.

DateDiff("n", Start_time, Finish_time)

The first parameter is a string that indicates the interval you are interested in using - it can be seconds, minutes, hours, days, weeks, months, years or a few other options.

Using n for this parameter indicates the difference is in minutes.

Surendra Parashar
Gurgaon,
http://www.geocities.com/surendraparashar

December 18th, 2007, 09:02 AM
 may29adil Registered User Join Date: Dec 2007 Posts: 9 Thanks: 0 Thanked 0 Times in 0 Posts

Thanks for reply, code is now working properly but there is a 1 more problem like if a person come online at 11/15/07 1:58PM and logout 11/15/07 2:10PM and Time display like 1hr and 12 Mint but the person login only for 12 Mints so please check it and let me know Thanks

December 19th, 2007, 02:52 AM
 woodyz Friend of Wrox Join Date: May 2006 Posts: 643 Thanks: 0 Thanked 0 Times in 0 Posts

Woody Z
http://www.learntoprogramnow.com
How to use a forum to help solve problems

 Similar Threads Thread Thread Starter Forum Replies Last Post convert time to minutes stolte XSLT 3 November 21st, 2008 04:12 AM Calculate time divovsky Access 2 March 15th, 2007 03:10 PM Calculate Time sswingle Classic ASP Basics 1 April 10th, 2006 08:30 AM calculate time between two dates RMorales2831 Access 1 February 13th, 2004 11:38 PM Calculate two times to find out how long they stay edkeyte Javascript How-To 0 January 19th, 2004 08:48 PM