What am I doing wrong??

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Penguin
Posts: 94
Joined: 26 Feb 2016, 16:02

What am I doing wrong??

29 Apr 2016, 07:53

I have tried over and over to calculate the number of days between to dates and I keep coming up with a blank. My dates are lastchangedate and it could be a month ago and the current data. I want to assign the number of days to a variable.
Here's the code

FormatTime, ld, lastChangeDate, yyyyMMdd

MsgBox, % "There are " DateDiff(ld, A_Now)
DateDiff( startdate, enddate)
{
units := "d"
enddate -= startdate, %units%
return enddate
}

thanks in advance
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: What am I doing wrong??

29 Apr 2016, 08:00

Don't format your date into the ld; you want the full YYYYMMDDHH24MISS timestamp when doing the date subtraction. As you didn't give a value for lastChangeDate, I can't explicitly test it, but I'm sure that's the (or a) problem.
Penguin
Posts: 94
Joined: 26 Feb 2016, 16:02

Re: What am I doing wrong??

29 Apr 2016, 08:05

My lastchange date variable comes in as text like this 4/21/16
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: What am I doing wrong??

29 Apr 2016, 08:18

Looks like you'll want this to convert your lastChangedate into a proper timestamp: https://autohotkey.com/board/topic/1876 ... dhh24miss/

Otherwise you can convert it yourself; parsing shouldn't be too tough with a limited (personal) use case.

Code: Select all

string:="4/21/16"
StringSplit, components, string, /
stamp:="20" SubStr("0" components3, -1) SubStr("0" components1, -1) SubStr("0" components2, -1) "000000" ; midnight of this day
MsgBox % stamp
return
You can pass stamp (or call it ld) into your function then.
User avatar
Flarebrass
Posts: 104
Joined: 20 Nov 2015, 13:13
Location: USA
Contact:

Re: What am I doing wrong??

29 Apr 2016, 08:27

Code: Select all

lastChangeDate := 20160419
;FormatTime, ld, lastChangeDate, yyyyMMdd
ld := lastChangeDate
;MsgBox, The old date is %ld%.

FormatTime, now, A_Now, yyyyMMdd
MsgBox, % "There are " . DateDiff(ld, now) . " days."
DateDiff(startdate, enddate)
{
    return enddate-startdate
}
ExitApp
enddate -= startdate is a complete statement. What you have after it, , %units% is erroneous and doesn't have any meaning. Also, you format the lastChangeDate but not A_Now, so those are in two different formats when you do the subtraction.

If that code doesn't work, then try Ex's string split method to format the time correctly at the beginning of my script.

The other issue that the number of days difference will be entirely wrong if the months are different. You need to include a type of calendar into your math in the DateDiff() function, and that's too much for me to code right now. My guess is maybe about 100 more lines of code for complete leap-year accuracy in the subtraction.
(Note that I can't test my code before posting, so beware of bugs! -Flarebrass Amatzikahni)
just me
Posts: 9464
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: What am I doing wrong??

29 Apr 2016, 08:41

Code: Select all

LastChangeDate := "4/21/16"
TimeStamp := Format("20{3:02}{1:02}{2:02}", StrSplit(LastChangeDate, "/")*)
MsgBox, % "There are " DateDiff(TimeStamp, A_Now) " days."
Return
DateDiff(startdate, enddate)
{
units := "d"
enddate -= startdate, %units%
return enddate
}
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: What am I doing wrong??

29 Apr 2016, 08:46

@FlareBrass, see EnvSub
User avatar
Flarebrass
Posts: 104
Joined: 20 Nov 2015, 13:13
Location: USA
Contact:

Re: What am I doing wrong??

29 Apr 2016, 09:46

I thought EnvAdd/EnvSub had to be explicitly called to work with dates. I'll have to start using that syntax!
(Note that I can't test my code before posting, so beware of bugs! -Flarebrass Amatzikahni)
Penguin
Posts: 94
Joined: 26 Feb 2016, 16:02

Re: What am I doing wrong??

29 Apr 2016, 12:04

Thanks everyone. I go it to work

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, Peiya, ShatterCoder and 313 guests