Date and Time Diff Question

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Date and Time Diff Question

Post by Rain » Thu Jun 20, 2013 3:18 pm

Shouldn't DateDiff return a negative value when the date is an earlier date?
The same goes for TimeDiff.

Code: Select all

GetDate>today
DateAdd>today,D,-1,yesterday
DateDiff>today,yesterday,D,NegativeDays
DateAdd>today,D,1,tomorrow
DateDiff>today,tomorrow,D,PositiveDays
mdl>Yesterday: %NegativeDays%%CRLF%Tomorrow: %PositiveDays%

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Re: Date and Time Diff Question

Post by armsys » Thu Jun 20, 2013 10:41 pm

Rain wrote:Shouldn't DateDiff return a negative value when the date is an earlier date?
The same goes for TimeDiff.
Hi RAIN,
DateDiff means "absolute" difference between 2 dates.
If the chronological order matters, try:

Code: Select all

GetDate>today
DateAdd>today,D,-1,yesterday
DateDiff>today,yesterday,D,NegativeDays
DateAdd>today,D,1,tomorrow
DateDiff>today,tomorrow,D,PositiveDays
mdl>Yesterday: %NegativeDays%%CRLF%Tomorrow: %PositiveDays%
Let>Sign={if(%Today%>%Yesterday%,-1,1)}
MDL>Sign
Let>NegativeDays=NegativeDays*Sign
MDL>NegativeDays
Hope it meets your requirement. Thnaks for sharing your discovery.

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Sun Jun 23, 2013 4:18 pm

Thanks for the reply, armsys.

I believe there is an issue with DateDiff because when I use DateDiff with VBScript I get a negative or positive result.

Marcus, is there an issue with DateDiff and TimeDiff?

Code: Select all

GetDate>Today
DateAdd>%Today%,d,-1,Yesterday
VBEval>DateDiff("d", "%Today%", "%Yesterday%"),NegativeDate

DateAdd>%Today%,D,1,Tomorrow
VBEval>DateDiff("d", "%Today%", "%Tomorrow%"),PositiveDate

mdl>Negative Date: %NegativeDate%%CRLF%Positive Date: %PositiveDate%

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Sun Jun 23, 2013 10:15 pm

Rain wrote:I believe there is an issue with DateDiff because when I use DateDiff with VBScript I get a negative or positive result.
Hi RAIN,
In actuality, there're plenty of nuanced differences between MS and VBscript. RegEx and HTTP are just 2 outstanding examples.
For DataDiff, maybe Marcus does MS users a service by yielding an absolute difference between 2 dates.
Furthermore, thanks to your thread, I expanded my test and discovered MS can't do simple date sum/difference:

Code: Select all

Let>d1=1/1/2013
Let>d2=1/2/2013
Let>d=d2-d1
It turns out that "d2-d1" is assigned to d.

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Mon Jun 24, 2013 11:57 am

Here is another example that includes times as well as dates.

Code: Select all

GetDate>Today
GetTime>Now

DateAdd>%Today%,D,1,Tomorrow
TimeAdd>%Now%,h,1,OneHourLater
VBEval>DateDiff("h", "%Today% %Now%", "%Tomorrow% %OneHourLater%"),DiffRes

mdl>Difference: %DiffRes% Hours

/*
Intervals:
yyyy - Year
q - Quarter
m - Month
y - Day of year
d - Day
w - Weekday
ww - Week of year
h - Hour
n - Minute
s - Second
*/
This example compares times only

Code: Select all

GetTime>Now

TimeAdd>%Now%,h,1,OneHourLater
VBEval>DateDiff("h", "%Now%", "%OneHourLater%"),DiffRes

mdl>Difference: %DiffRes% Hour

/*
Intervals:
yyyy - Year
q - Quarter
m - Month
y - Day of year
d - Day
w - Weekday
ww - Week of year
h - Hour
n - Minute
s - Second
*/

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Mon Jun 24, 2013 12:48 pm

Rain wrote:Here is another example that includes times as well as dates.
Hi RAIN,
Thanks for 2 more scripts on date/time differences.
In particular, I like your handy documentation at the bottom.
I'm now convinced that MS should support "signed" DateDiff command.
Reason:
1. Positive/negative difference between 2 dates is often important.
2. Save additional statements to determine the positive/negative difference.
Hope Marcus will respond your feature request.
Thanks again.

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Mon Jun 24, 2013 3:22 pm

I'm curious if there is a bug in DateDiff and TimeDiff or if Marcus and crew meant to code it this way. Either way, I don't mind using the vbScript examples I posted.

User avatar
Grovkillen
Automation Wizard
Posts: 1132
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Post by Grovkillen » Mon Jun 24, 2013 8:09 pm

It would be great if I could tell MS that I want absolute or relative datediff. Maybe by a:

Let>DATEDIFF_STATE=1 (for relative)

and

Let>DATEDIFF_STATE=0 (for absolute, default)

Same for timediff...
Let>ME=%Script%

Running: 15.0.27
version history

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Mon Jun 24, 2013 10:11 pm

Rain wrote:Either way, I don't mind using the vbScript examples I posted.
Agree. Rain, thanks for your VBScript scripts, which benefit us all.

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Mon Jun 24, 2013 10:14 pm

Grovkillen wrote:Let>DATEDIFF_STATE=1 (for relative)
and
Let>DATEDIFF_STATE=0 (for absolute, default)
Same for timediff...
Excellent idea. I love Swedish design-simple and practical. Thanks.

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