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
What am I doing wrong??
Re: What am I doing wrong??
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.
Re: What am I doing wrong??
My lastchange date variable comes in as text like this 4/21/16
Re: What am I doing wrong??
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.
You can pass stamp (or call it ld) into your function then.
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
- Flarebrass
- Posts: 104
- Joined: 20 Nov 2015, 13:13
- Location: USA
- Contact:
Re: What am I doing wrong??
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
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)
Re: What am I doing wrong??
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
}
Re: What am I doing wrong??
@FlareBrass, see EnvSub
- Flarebrass
- Posts: 104
- Joined: 20 Nov 2015, 13:13
- Location: USA
- Contact:
Re: What am I doing wrong??
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)
Re: What am I doing wrong??
Thanks everyone. I go it to work
Who is online
Users browsing this forum: Araphen, Peiya, ShatterCoder and 313 guests