Date Difference

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
daniel
Junior Coder
Posts: 32
Joined: Mon Apr 25, 2005 5:04 pm

Date Difference

Post by daniel » Tue May 22, 2007 2:15 pm

I'm working in a macro where a person enters one date in the following format: mmddyy. If the difference from today's date and the day keyed isn't greater than 19, I want a message to pop up.

For example, the first date keyed was 050491 (May 4, 1991) and today's date is 052207 (May 22, 2007), since the difference in these two dates is not greater than 19, I would like a my macro to stop at this time.

All I need help is calculating these date difference in years. thanks in advance.

User avatar
pgriffin
Automation Wizard
Posts: 460
Joined: Wed Apr 06, 2005 5:56 pm
Location: US and Europe

Post by pgriffin » Tue May 22, 2007 2:22 pm

time to think about learning a bit of VBScript.

DateDiff(interval, date1, date2 [,firstdayofweek[, firstweekofyear]])

http://msdn.microsoft.com/library/defau ... 58c8fa.asp

I would "trust" vbscript to handle this type function. There will be all manner of suggestions for writing "creative" MacroScript code (which I do as well) but when VBScript fits something this nicely, it is best to use it.

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

Post by Marcus Tettmar » Tue May 22, 2007 2:36 pm

Here you go:

Code: Select all

VBSTART
VBEND

Let>KeyedDate=050491

//Note DateSerial interprets a year value of between 00 and 99 as being 1900 to 1999
//If you want the user to supply a year after 1999 you need the format to be
//mmddyyyy not mmddyy as you have specified.  This code will work whichever of those formats is used:
Length>KeyedDate,ld
MidStr>KeyedDate,5,ld,YY
MidStr>KeyedDate,1,2,MM
MidStr>KeyedDate,3,2,DD

VBEVal>DateDiff("yyyy",DateSerial(%YY%,%MM%,%DD%),now),yDif
If>yDif<20
  MessageModal>Date entered %yDif% years ago
Endif
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

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 May 22, 2007 2:47 pm

I suspect that code doesn't do what Daniel is asking. My guess would be the request is for something like checking age based on birthday, in which case (today) 052191 would be 16 years and 052391 would be 15 years.

daniel
Junior Coder
Posts: 32
Joined: Mon Apr 25, 2005 5:04 pm

Post by daniel » Tue May 22, 2007 4:52 pm

Tha's correct... I am using it for age verification so I would need to know the actual age of that person today. thanks.

jennylci
Newbie
Posts: 14
Joined: Wed May 08, 2013 11:16 pm

Post by jennylci » Fri Sep 20, 2013 6:50 pm

Hi Marcus,

I can't find DateDiff function from index searching on my macroscheduler. So I was trying to use your VB code but macroscheduler gives me this error message. "Invalid procedure call or argument: 'DateDiff'". Could you please advise what I can do about it or is there any other way to do it?

User avatar
JRL
Automation Wizard
Posts: 3505
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Sun Sep 22, 2013 4:45 am

In 2007 when this thread started there was only VBScript for datediff. But starting with version 13 ther is a built in datedaff function. See the on-line help for more information.

jennylci
Newbie
Posts: 14
Joined: Wed May 08, 2013 11:16 pm

Post by jennylci » Mon Sep 23, 2013 4:33 pm

Mine is Microscheduler version 11.

User avatar
JRL
Automation Wizard
Posts: 3505
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Mon Sep 23, 2013 5:00 pm

Here is the above script with some mods. What do you see in the message boxes?

Code: Select all

VBSTART
VBEND

Let>KeyedDate=050491

//Note DateSerial interprets a year value of between 00 and 99 as being 1900 to 1999
//If you want the user to supply a year after 1999 you need the format to be
//mmddyyyy not mmddyy as you have specified.  This code will work whichever of those formats is used:
Length>KeyedDate,ld
MidStr>KeyedDate,5,ld,YY
MidStr>KeyedDate,1,2,MM
MidStr>KeyedDate,3,2,DD

VBEval>DateSerial(%YY%,%MM%,%DD%),res
VBEvaL>now,res2

MDL>%res%  %res2%

VBEVal>DateDiff("yyyy",DateSerial(%YY%,%MM%,%DD%),now),yDif

MDL>ydif

If>yDif<20
  MessageModal>Date entered %yDif% years ago
Endif

jennylci
Newbie
Posts: 14
Joined: Wed May 08, 2013 11:16 pm

Post by jennylci » Mon Sep 23, 2013 7:23 pm

Code: Select all

day>DD
month>MM
year>YYYY
VBSTART
VBEND

GetFileList>C:\Temp\google_*.txt.pgp,files
Separate>files,;,file_names
let>k=0
let>fdate_0=0
Repeat>k
    let>k=k+1
    let>file=file_names_%k%
    Separate>file_names_%k%,\,check_file
    let>filename=check_file_3
    FileDate>%file%,fdate
    MidStr>fdate,1,4,y1
    MidStr>fdate,5,2,m1
    MidStr>fdate,7,2,d1
    VBEVal>DateSerial(%y1%,%m1%,%d1%),res
    VBEVal>now,res2
    MDL>%res%,%res2%
    VBEVal>DateDiff("dd",res,now),Ddif
    MDL>%Ddif%
    if>DDif>15
    goto>email
    endif
Until>k,file_names_count
Label>email
writeln>Y:\Text\Prod_Info.txt,res,subj:*********google file is not created on %YYYY%%MM%%DD%
Label>done
[quote]
What I'm trying to do is find the file whose name is starting with google and see if it's older than 15 days. If it is then I will send out an email. Unfortunately, macroscheduler 11 doesn't recognize datediff function, it still gives me the same error message. Any suggestion?[/quote]

User avatar
JRL
Automation Wizard
Posts: 3505
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Mon Sep 23, 2013 7:40 pm

Close

In the datediff line change the time interval parameter to a single "d" and also put percents and quotes around the "res" variable

Code: Select all

VBEVal>DateDiff("d","%res%",now),Ddif

jennylci
Newbie
Posts: 14
Joined: Wed May 08, 2013 11:16 pm

Post by jennylci » Mon Sep 23, 2013 9:46 pm

Thank you very much, JRL! It finally ran through!

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