Time in minutes.
Moderators: JRL, Dorian (MJT support)
Time in minutes.
Hi, I was just wondering if it is possible to do manipulation involving time.
I have a macro that does long series of operations (until "break" the operation). Each operations take a different amount of time to do (and the Macro would "Wait" accordingly as to do the tasks efficiently).
What I'd like to do, is to be able to keep track of the amount of time elapsed (say, in minute), so that I can calculate the average amount of time it takes to do all operations.
Basically, I'd like to do counter (of the number of operations so far)/time (in minute). Getting a counter for the amount of operation done is easy, but I am not too sure how to get the total amount of time elapsed in minute (or hours, or seconds).
Any help would be much appreciated.
Thanks.
I have a macro that does long series of operations (until "break" the operation). Each operations take a different amount of time to do (and the Macro would "Wait" accordingly as to do the tasks efficiently).
What I'd like to do, is to be able to keep track of the amount of time elapsed (say, in minute), so that I can calculate the average amount of time it takes to do all operations.
Basically, I'd like to do counter (of the number of operations so far)/time (in minute). Getting a counter for the amount of operation done is easy, but I am not too sure how to get the total amount of time elapsed in minute (or hours, or seconds).
Any help would be much appreciated.
Thanks.
- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
No time for script right now but here is a structure:
Hour>StartHour
Min>StartMin
...Do other things in macro
Hour>EndHour
Min>EndMin
Let>ElapsedHours=%EndHour%-%StartHour%
Let>ElapsedMins=%EndMin%-%StartMin%
Let>ElapsedHourMins=%ElapsedHours% * 60
Let>ElapsedMins=%ElapsedMins% + %ElapsedHourMins%
You will need to modify to check for hours after midnight. and when minutes go from one hour into another, like add 60 mins for every change in every hour over one, if 2 or more hours. If only one hour difference then take (60-StartMin) and add EndMin
Hour>StartHour
Min>StartMin
...Do other things in macro
Hour>EndHour
Min>EndMin
Let>ElapsedHours=%EndHour%-%StartHour%
Let>ElapsedMins=%EndMin%-%StartMin%
Let>ElapsedHourMins=%ElapsedHours% * 60
Let>ElapsedMins=%ElapsedMins% + %ElapsedHourMins%
You will need to modify to check for hours after midnight. and when minutes go from one hour into another, like add 60 mins for every change in every hour over one, if 2 or more hours. If only one hour difference then take (60-StartMin) and add EndMin
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!
Bob
A humble man and PROUD of it!
To get elapsed time use the VBScript DateDiff function. See the VBScript language reference. Here's an example:
VBSTART
VBEND
//At start of script/process get current time
VBEval>Now(),StartTime
do lots of stuff here
//compare current time to start time
VBEval>datediff("s","%StartTime%",Now()),SecsElapsed
VBEval>datediff("n","%StartTime%",Now()),MinsElapsed
VBEval>datediff("h","%StartTime%",Now()),HoursElapsed
Links to VBScript documentation here:
http://www.mjtnet.com/resources.htm
VBSTART
VBEND
//At start of script/process get current time
VBEval>Now(),StartTime
do lots of stuff here
//compare current time to start time
VBEval>datediff("s","%StartTime%",Now()),SecsElapsed
VBEval>datediff("n","%StartTime%",Now()),MinsElapsed
VBEval>datediff("h","%StartTime%",Now()),HoursElapsed
Links to VBScript documentation here:
http://www.mjtnet.com/resources.htm
MJT Net Support
[email protected]
[email protected]
- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
Just a small enhancement to the VBScript solution above. I used it last night with the following values
Time1 = 04/17/05 01:53:33 AM
Time2 = 04/17/05 06:05:24 AMThis resulted in message that said this:
Total elapsed times was
5 hours, 252 minutes, 15111 seconds.
-----------------------------------------------------
I was surprised at first, but realized that I got what I asked for.
So I added some lines to give me the results that I really wanted. I added three lines immediately after the previous VB lines.Now I got the message with the correct value formats that I wanted:
Total elapsed times was
5 hours, 12 minutes, 51 seconds.
================================
Edited note:
ERROR in previous posting of the formulas above.
Format is correct, but values are wrong.
Actual elapsed time should be 4 hours,, 11 mins, 51 seconds.
Need to modify formulas.
It appears that MOD only brings back the absolute differential between the time values, not the actual time.
If Time1 was 1:47:55 and Time2 was 1:48:05, a difference of only 10 seconds, DateDiff for Minutes would return 1 minute, because the minute value changed.
A similar problem happens with DateDiff for Hours. If Time1 was 3:59:50 and Time2 was 4:03:57 would return 1 hour even though only about 4 minutes went by.
See next message for corrrect script for Elapsed Time in Hours, Minutes, Seconds.
Time1 = 04/17/05 01:53:33 AM
Time2 = 04/17/05 06:05:24 AM
Code: Select all
VBEval>datediff("s","%Time1%","%Time2%"),SecsElapsed
VBEval>datediff("n","%Time1%","%Time2%"),MinsElapsed
VBEval>datediff("h","%Time1%","%Time2%"),HoursElapsed
Let>TotalTime=%HoursElapsed% hours, %MinsElapsed% minutes, %SecsElapsed% seconds
Message>Total elapsed times was %CRLF%%TotalTime%.
Total elapsed times was
5 hours, 252 minutes, 15111 seconds.
-----------------------------------------------------
I was surprised at first, but realized that I got what I asked for.
So I added some lines to give me the results that I really wanted. I added three lines immediately after the previous VB lines.
Code: Select all
VBEval>datediff("s","%Time1%","%Time2%"),SecsElapsed
VBEval>datediff("n","%Time1%","%Time2%"),MinsElapsed
VBEval>datediff("h","%Time1%","%Time2%"),HoursElapsed
//Use Modulo to get "common usage" values.
VBeval>("%MinsElapsed%" Mod "60"),MinsElapsed
VBeval>("%SecsElapsed%" Mod "3600"),SecsElapsed
VBeval>("%SecsElapsed%" Mod "60"),SecsElapsed
Let>TotalTime=%HoursElapsed% hours, %MinsElapsed% minutes, %SecsElapsed% seconds
Message>Total elapsed times was %CRLF%%TotalTime%.
Total elapsed times was
5 hours, 12 minutes, 51 seconds.
================================
Edited note:
ERROR in previous posting of the formulas above.
Format is correct, but values are wrong.
Actual elapsed time should be 4 hours,, 11 mins, 51 seconds.
Need to modify formulas.
It appears that MOD only brings back the absolute differential between the time values, not the actual time.
If Time1 was 1:47:55 and Time2 was 1:48:05, a difference of only 10 seconds, DateDiff for Minutes would return 1 minute, because the minute value changed.
A similar problem happens with DateDiff for Hours. If Time1 was 3:59:50 and Time2 was 4:03:57 would return 1 hour even though only about 4 minutes went by.
See next message for corrrect script for Elapsed Time in Hours, Minutes, Seconds.
Last edited by Bob Hansen on Sun Apr 17, 2005 8:39 pm, edited 1 time in total.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!
Bob
A humble man and PROUD of it!
- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
OK, I think I have the solution now.
Just need the value in seconds and convert that into minutes and hours, using the remainders as needed.
This provides Elapsed Time in hours, minutes, seconds, based on Time1 and Time2 collected earlier.
This is based on time in this format that included the date:
Time1 = 04/17/05 01:53:33 AM
Time2 = 04/17/05 06:05:24 AM
Now I got the message with the values that I wanted for 15111 seconds:
Total elapsed times was
4 hours, 11 minutes, 51 seconds.
Just need the value in seconds and convert that into minutes and hours, using the remainders as needed.
This provides Elapsed Time in hours, minutes, seconds, based on Time1 and Time2 collected earlier.
This is based on time in this format that included the date:
Time1 = 04/17/05 01:53:33 AM
Time2 = 04/17/05 06:05:24 AM
Code: Select all
VBStart
VBEnd
VBEval>datediff("s","%Time1%","%Time2%"),SecsElapsed
Label>MakeMins
Let>MinsElapsed={Int(%SecsElapsed%/60)}
If>%MinsElapsed%>0,CalcSecs,CalcHours
Label>CalcSecs
VBeval>("%SecsElapsed%" Mod "60"),SecsElapsed
Label>CalcHours
Let>HoursElapsed={Int(%MinsElapsed%/60)}
If>%HoursElapsed%>0,CalcMins,Msg
Label>CalcMins
VBeval>("%MinsElapsed%" Mod "60"),MinsElapsed
Label>Msg
Let>TotalTime=%HoursElapsed% hours, %MinsElapsed% minutes, %SecsElapsed% seconds
Message>Total elapsed times was %CRLF%%TotalTime%.
Total elapsed times was
4 hours, 11 minutes, 51 seconds.
Last edited by Bob Hansen on Wed Dec 06, 2006 9:34 pm, edited 2 times in total.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!
Bob
A humble man and PROUD of it!
- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
Hello Joe:
Is Time2 in the correct format?
This sample code was based on a Time format that includes date and time like this: 12/06/06 16:35:13
VB is looking for values in that format.
If your variable is not in that format you will probably have a problem. You may need to parse your variable to look like the VB Time format. It has been a while since I wrote this, but the Time format is defined by VB not by Macro Scheduler,
Using the original sample:
Let>Time1=12/15/06 23:10:03
Let>Time2=12/16/06 02:11:04
The total seconds = 10861, but.....if you do this:
Let>Time1=23:10:03
Let>Time2=02:11:04
Removing the date, the total seconds = 75539
My original code when I made Time1 and Time2 looked like this:
VBEVAL>Now(),Time1
where Now() took the system date/time as a value.
Is Time2 in the correct format?
This sample code was based on a Time format that includes date and time like this: 12/06/06 16:35:13
VB is looking for values in that format.
If your variable is not in that format you will probably have a problem. You may need to parse your variable to look like the VB Time format. It has been a while since I wrote this, but the Time format is defined by VB not by Macro Scheduler,
Using the original sample:
Let>Time1=12/15/06 23:10:03
Let>Time2=12/16/06 02:11:04
The total seconds = 10861, but.....if you do this:
Let>Time1=23:10:03
Let>Time2=02:11:04
Removing the date, the total seconds = 75539
My original code when I made Time1 and Time2 looked like this:
VBEVAL>Now(),Time1
where Now() took the system date/time as a value.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!
Bob
A humble man and PROUD of it!
Hi Marcus and Bob,
Thank you for the code. Below is a version where the user does not have to worry about date/time formats... get both using VBScript and it always works.
Incidently, I had an error similar to what JBurger had and it turned out to be I was missing a percent sign % on one side of the variable in the datediff line.
Once again, thanks Marcus and Bob for this code... added to snippets!
Thank you for the code. Below is a version where the user does not have to worry about date/time formats... get both using VBScript and it always works.
Code: Select all
//Calculating Elaspsed Time
//http://www.mjtnet.com/usergroup/viewtopic.php?t=3463
VBSTART
VBEND
//At start of script/process get current time
VBEval>Now(),StartTime
//do lots of stuff here (Wait value is just for testing)
Wait>3
//compare current time to start time
VBEval>datediff("s","%StartTime%",Now()),SecsElapsed
Label>MakeMins
Let>MinsElapsed={Int(%SecsElapsed%/60)}
If>%MinsElapsed%>0,CalcSecs,CalcHours
Label>CalcSecs
VBeval>("%SecsElapsed%" Mod "60"),SecsElapsed
Label>CalcHours
Let>HoursElapsed={Int(%MinsElapsed%/60)}
If>%HoursElapsed%>0,CalcMins,Msg
Label>CalcMins
VBeval>("%MinsElapsed%" Mod "60"),MinsElapsed
Label>Msg
Let>TotalTime=%HoursElapsed% hours, %MinsElapsed% minutes, %SecsElapsed% seconds
Message>Total elapsed times was %CRLF%%TotalTime%.
Once again, thanks Marcus and Bob for this code... added to snippets!

jpuziano
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -

- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
Thanks fot the tool to capture the times and calculate. This is similar to what I did with my original testing, but jst referred to it vs. showing it. I only mentioned the result as Time1 and Time2.
But what JBurger is looking for sounds like he may have imported the time from some other process, and assigned it to Time2. So he will still need to parse the time into the correct format.
The VB Now() functions generates that format automatically, but if that function is not used and not collected real time, some type of parsing will be needed.
And yes, if a % is missed, then a wrong value will be passed in to VB.
Thanks again for all your help, jpuziano
But what JBurger is looking for sounds like he may have imported the time from some other process, and assigned it to Time2. So he will still need to parse the time into the correct format.
The VB Now() functions generates that format automatically, but if that function is not used and not collected real time, some type of parsing will be needed.
And yes, if a % is missed, then a wrong value will be passed in to VB.
Thanks again for all your help, jpuziano
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!
Bob
A humble man and PROUD of it!
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Bob,
You don't need to do any parsing at all. Use VBScript's TimeValue or DateValue functions to return a string representation of a time/date to a VBScript time/date value:
http://msdn.microsoft.com/library/en-us ... 6a98f6.asp
"The time argument is usually a string expression representing a time from 0:00:00 (12:00:00 A.M.) to 23:59:59 (11:59:59 P.M.), inclusive. However, time can also be any expression that represents a time in that range. If time contains Null, Null is returned.
You can enter valid times using a 12-hour or 24-hour clock. For example, "2:24PM" and "14:24" are both valid time arguments. If the time argument contains date information, TimeValue doesn't return the date information. However, if time includes invalid date information, an error occurs."
So TimeValue will cope with the OPs string of "12/06/06 16:35:13".
You don't need to do any parsing at all. Use VBScript's TimeValue or DateValue functions to return a string representation of a time/date to a VBScript time/date value:
http://msdn.microsoft.com/library/en-us ... 6a98f6.asp
"The time argument is usually a string expression representing a time from 0:00:00 (12:00:00 A.M.) to 23:59:59 (11:59:59 P.M.), inclusive. However, time can also be any expression that represents a time in that range. If time contains Null, Null is returned.
You can enter valid times using a 12-hour or 24-hour clock. For example, "2:24PM" and "14:24" are both valid time arguments. If the time argument contains date information, TimeValue doesn't return the date information. However, if time includes invalid date information, an error occurs."
So TimeValue will cope with the OPs string of "12/06/06 16:35:13".
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
P.S. Please don't forget about the online VBScript language reference at:
http://msdn.microsoft.com/library/defau ... 52fc54.asp
Or download it from:
http://www.microsoft.com/downloads/deta ... laylang=en
More details:
http://www.mjtnet.com/resources.htm
http://msdn.microsoft.com/library/defau ... 52fc54.asp
Or download it from:
http://www.microsoft.com/downloads/deta ... laylang=en
More details:
http://www.mjtnet.com/resources.htm
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
I am not being clear, I apologize.
I understand that VB provides the correct format.
In jpuliano's example that is not needed because the time is coming from Now() with the VB command. I was addressing the code that I submitted originally that already had Time1 and Time2 predefined.
JBerger does not state where he got Time2 from, but it probably was not from Now() if he used my example as it is.
I was trying to point out that if a variable, "Time2", is acquired in a different method, then it would need to be parsed for VB to use it properly.
For example, suppote we use ReadLn on a log file from some program and we extract a time of 21:43:16 and assign it to Time2. That may end up with the wrong result, especially if going through midnight. To make this work properly we need to add mm/dd/yy to the front of it. So the value of Time2 would be mm/dd/yy 21:43:16 That is the type of parsing I am talking about.
The original elapsed time function was intended to be used for calculations within a 24 hour period, but could go from day1 to day2 through midnight. It would need another section of code to calculate days if the time period was over 24 hours. But that is another reason why it is iimportant to have the date as part of the variable.
I understand that VB provides the correct format.
In jpuliano's example that is not needed because the time is coming from Now() with the VB command. I was addressing the code that I submitted originally that already had Time1 and Time2 predefined.
JBerger does not state where he got Time2 from, but it probably was not from Now() if he used my example as it is.
I was trying to point out that if a variable, "Time2", is acquired in a different method, then it would need to be parsed for VB to use it properly.
For example, suppote we use ReadLn on a log file from some program and we extract a time of 21:43:16 and assign it to Time2. That may end up with the wrong result, especially if going through midnight. To make this work properly we need to add mm/dd/yy to the front of it. So the value of Time2 would be mm/dd/yy 21:43:16 That is the type of parsing I am talking about.
The original elapsed time function was intended to be used for calculations within a 24 hour period, but could go from day1 to day2 through midnight. It would need another section of code to calculate days if the time period was over 24 hours. But that is another reason why it is iimportant to have the date as part of the variable.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!
Bob
A humble man and PROUD of it!
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
But it won't. The TimeValue and DateValue commands can recognise a string version of a date or time and turn it into a VBScript date or time. They will cope with your example. No parsing is needed.I was trying to point out that if a variable, "Time2", is acquired in a different method, then it would need to be parsed for VB to use it properly.
But I see your point that you may need to add the date to the time when spanning days. That's a different issue.
I keep seeing suggestions on this forum to modify your regional date settings. I'm trying to combat that - it is NOT necessary and no one should have to do it. Using the date functions in MacroScript and VBScript you can make portable scripts.
[/quote]
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?