Comparing Dates

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Monoxide3009
Posts: 65
Joined: 09 Apr 2018, 15:53

Comparing Dates

19 Jul 2018, 12:45

I have seen MANY forum posts on this, so I apologize, but I cannot get what I am doing wrong even with some of those answers. I am still quite new, so any advice would help if extremely detailed. Do not worry about condescending =p

Right now, I am trying to compare a date pulled from an internal program to today's current date. I thought this would be a lot easier than it has been. I 'believe' the problem lies with the date being pulled internally is set as text.

My current code is:

Code: Select all

var1 = %A_Now% ; current date
FormatTime, var1, %var1%, MM/dd/yyyy ; set to just date, no time
Send, %var1%`r ; confirm output
Sleep, 50
var2 = %clipboard% ; pulls "text" date as 07/15/2018 as test
Sleep, 50
FormatTime, var2, %var2%, MM/dd/yyyy ; does not convert to format properly, if this line removed, "date" is an output, but then EnvSub does not work.
Send, %var2%`r ; confirm output

EnvSub, var1, %var2%, days ; expcted outcome to be 4 with current date
MsgBox, %var1% ; currently has blank output
I have tried looking into ParseDate(), but my version does not recognize that as a function, and getting updates from IT here is like pulling teeth. Any help on getting this date function to work would be greatly appreciated.

My overall goal is to have an - if (datedifference > 40) {alternate route} - option within my current code. If there is a better/easier way to accomplish this, by all means, let me know.
Monoxide3009
Posts: 65
Joined: 09 Apr 2018, 15:53

Re: Comparing Dates

19 Jul 2018, 15:49

Playing around, I used a regex match to change the format of both dates to yyyymmdd then used EnvSub, and it seems to be working.

I am going to try to adapt this to the main script.

If anyone has suggestions or a better working process, please let me know still.

What I came up with:

var1 = %A_Now% ; current date
FormatTime, var1, %var1%, yyyyMMdd ; set to just date, no time
Send, %var1%`r ; confirm output
Sleep, 50
var2 = %clipboard% ; pulls "text" date as 07/15/2018
Sleep, 50
RegExMatch(var2, "(..)\/(..)\/(....)", var2)
Sleep, 50
var2 = %var23%%var21%%var22%
Sleep, 50
Send, %var2% ; confirmation

FormatTime, var2, %var2%, yyyyMMdd


EnvSub, var1, %var2%, days
MsgBox, %var1% ; currently has blank output
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: Comparing Dates

19 Jul 2018, 18:36

Hi, this is an interesting topic. There are many ways to skin this cat. Another is below.
Note I didn't use the clipboard, I just set a variable to a date. For me regexes are still black magic, but I'm overjoyed when I get one that works.
I used RegExReplace and backreferences instead of RegExMatch. I'm not sure it matters.

Code: Select all

var1 = %A_Now% ; current date
FormatTime, var1, %var1%, yyyyMMdd ; set to just date, no time
msgbox, "Now" is %var1%
;var2 = 07/14/2015
;var2 = 07 14 2015
var2 = 07-14-2015
var2 := RegExReplace(var2, "(\d{2})[-\s//](\d{2})[-\s//](\d{4})", "$3$1$2")
;Re-edit 7/20/18. Changed first two matches to be just 2 digits.
;Edit: I think the last match should be just (\d{4}) instead of like the first 2, otherwise you could
;match 05/05/15, which of course won't work.
Msgbox, var2 is now %var2% ; confirmation

FormatTime, var2, %var2%, yyyyMMdd

msgbox var1 is %var1%
msgbox var2 is %var2%
;EnvSub, var1, %var2%, days
; can also be as below
;var1 -= var2, days ; or, disconcertingly enough
var1 -= %var2%, days
MsgBox, The difference is %var1% days
Regards,
burque505

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: haomingchen1998, robodesign, xask1 and 239 guests