Subtracting one time stamp from another

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Rory
Pro Scripter
Posts: 50
Joined: Thu Mar 23, 2006 2:50 pm
Location: Wisconsin

Subtracting one time stamp from another

Post by Rory » Mon Dec 08, 2008 11:32 pm

Is it possible to subtract one time stamp from another?
For example, I am interested in finding the difference between 07:06:44 and 07:00:00.

Thanks much,
Rory

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Mon Dec 08, 2008 11:37 pm

I find it best to use VBScript for DateDiff

Here is a line calculating elapsed time in minutes:
VBEval>DateDiff("n","%timStart%","%timStop%"),numMinutes

timStart and timStop are filled using GetTime>
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Rory
Pro Scripter
Posts: 50
Joined: Thu Mar 23, 2006 2:50 pm
Location: Wisconsin

Post by Rory » Tue Dec 09, 2008 4:09 am

Thanks Bob,
It does work but rounds the time to the nearest minute. Is it possible to include the seconds too?

Rory

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Dec 09, 2008 6:22 am

For measuring elapsed time I always use VBScript's Timer function which returns seconds since midnight. Therefore easy to subtract start from finish and get elapsed seconds. Lots of examples here - search site for Timer.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Tue Dec 09, 2008 7:09 pm

I have not used Timer, but seem to recall that there may be a problem if using times that go past midnight, using different dates. That is why I end up using DateDiff instead. Multiple dates are taken care of automatically.

To get seconds, use "s":

Code: Select all

VBEval>DateDiff("s","%timStart%","%timStop%"),numSeconds
But this will be total seconds. You will need to take the remainder of seconds to use with minutes, like this:

Code: Select all

VBEval>DateDiff("n","%timStart%","%timStop%"),numMinutes 
VBEval>DateDiff("s","%timStart%","%timStop%"),numSeconds
\\ Help file does not have a sample of using MOD operator.  May not work.
Let>numSeconds={Mod(%NumSeconds%,60)}
Let>ElapsedTime=%numMinutes%:%numSeconds%
Last edited by Bob Hansen on Tue Dec 09, 2008 10:57 pm, edited 1 time in total.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Rory
Pro Scripter
Posts: 50
Joined: Thu Mar 23, 2006 2:50 pm
Location: Wisconsin

Post by Rory » Tue Dec 09, 2008 8:37 pm

Thanks Bob,
I can not seem to get the following line to work for me:

Let>numSeconds={Mod(%numSeconds%,60)}

I get an error message "opMod not allowed here"
Any suggestions?

Rory

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue Dec 09, 2008 9:42 pm

Mod is confusing, it's:

Let>numSeconds={%numSeconds% mod 60}

Rory
Pro Scripter
Posts: 50
Joined: Thu Mar 23, 2006 2:50 pm
Location: Wisconsin

Post by Rory » Tue Dec 09, 2008 9:52 pm

Thanks for the help. It works just like I needed it to.

Rory

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Tue Dec 09, 2008 10:56 pm

Glad it works Rory.

And thanks for the syntax Me_again. As I noted in my script, the syntax for MOD was unknown to me.

Marcus, I think we need to have sample with syntax format added to Help file with Arithmetic Operators. Also need example for DIV? Is it the same as MOD?
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue Dec 09, 2008 11:46 pm

Yes, div is the same:

Let>qq={12345 div 100}
MDL>qq

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Wed Dec 10, 2008 1:42 am

Hmm, I have come to accept that the syntax is implied. MOD and DIV are both arithmetic OPERATORS, not functions. No different from +. -, -, / symbols. They are used the same way, value1 operator value2.

I am used to other languages where MOD is more of a function like INT, ABS, ROUND, etc.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed Dec 10, 2008 9:51 am

Bob Hansen wrote:Hmm, I have come to accept that the syntax is implied. MOD and DIV are both arithmetic OPERATORS, not functions. No different from +. -, -, / symbols. They are used the same way, value1 operator value2.
I was going to say the same thing. They are documented in the help file under "Complex Expressions" -> "Arithmetic Operators" along with the easy ones: +, - etc
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts