Page 1 of 2
If var is a date, how?
Posted: 21 Jun 2022, 10:05
by Krd
Hey,
How to fix this?
Code: Select all
Myvar := "13.12.2020" ;always a valid date and dd.mm.yyyy
if my Myvar is a date
msgbox, A valid date.
Else
msgbox, not a valid date
Aprreciate any help
Re: If var is a date, how?
Posted: 21 Jun 2022, 10:21
by RussF
First, "31.12.2020" is NOT a valid date as far as AHK is concerned. A DateTime String is in the format of YYYYMMDDHH24MISS.
Second,
should be
per the
If Var is Type documentation.
You must provide the command with a date value in the correct format.
Russ
Re: If var is a date, how?
Posted: 21 Jun 2022, 10:40
by Krd
Okey
So no way to know if the variable is a date or not?
Fixed date in variable...
This is not a reliable way if other strings or numbers are also 10 in number:
Myvar := "13.12.2020"
MyvarNew := StrLen(Myvar)
If (MyvarNew ="10")
Msgbox, Maybe a valid date
Else
Msgbox, Not a valid date
If Myvar := "1234567891" then it would fail....
Re: If var is a date, how?
Posted: 21 Jun 2022, 10:49
by BoBo
Na ja, if you validate its pieces and those are matching a specified pattern, yes you can proclaim that a number of integers separated by whatever characters is a date.
In the same way e-mail addresses are getting validated. Zum Beispiel with RegExMatch(). HTH
Re: If var is a date, how?
Posted: 21 Jun 2022, 10:51
by Krd
@BoBo
You are good at arrays.
Thats what you say could work for me. Any more specific ideas? The pattern if true is always that.
Re: If var is a date, how? Topic is solved
Posted: 21 Jun 2022, 11:04
by BoBo
Erm, I'm fairly good at some things - ie falling asleep!
Well, RegEx isn't part of it. And doing it with a conglomerate of AHK commands just to bypass a single magic line of RegEx?
That would be noob'ish by nature. I'd bet that while I'm writing this line, someone has posted that RegEx already...
Hier dürfte wohl nur eine Änderung des Separators anstehen:
viewtopic.php?p=259401#p259401 (fingers crossed)
Re: If var is a date, how?
Posted: 21 Jun 2022, 11:42
by Krd
It has to be this.... Boooring
Code: Select all
Myvar := "23.05.2022"
MyvarNew := StrLen(Myvar)
d := StrSplit(Myvar,".")
if (1 < d.1 < 31)
DayTest := true
if (1 < d.2 < 12)
MonthTest := true
if (1900 < d.3 < 2100)
YearTest := true
If (MyvarNew ="10")
Length := true
if (YearTest = true) && (MonthTest = true) && (DayTest = true) && (Length = true)
Msgbox, A valid date
Else Msgbox, Not a valid date
return
Re: If var is a date, how?
Posted: 21 Jun 2022, 11:51
by BoBo
@Krd
Nope, it's still not a valid date as '31.02.2022' isn’t.
Therefore it's not mandatory to check for the number-ranges 1-31/1-12, but if it's of (one or) two (=day&month) or (two or) four (=year) digits (and for valid separators).
Once the validation is done rearrange its parts to become a valid YYYYMMDDHH24MISS string that can be further processed ie with FormatTime.
Code: Select all
F12::
d := StrSplit("99.99.9999",".")
t := (StrLen(d.1)=2)
&& (StrLen(d.2)=2)
&& (StrLen(d.3)=4)
? d.3 . d.2 . d.1 . "000000"
: "Nope!"
if t is digit
MsgBox % t
else
SoundBeep
FormatTime, date,% t, dd.MM.yyyy
MsgBox % res := (date="") ? "Invalid datestamp" : date
return
Tested.
Re: If var is a date, how?
Posted: 21 Jun 2022, 12:29
by Krd
Not working
Re: If var is a date, how?
Posted: 21 Jun 2022, 12:36
by BoBo
@Krd
Well, it does (bc I've tested it meanwhile and silently changed the code)
Conclusio(n):
"if var is a date? Nope!"
"if var fits a specific date pattern? Yep."
Re: If var is a date, how?
Posted: 21 Jun 2022, 12:51
by BoBo
And,
@teadrinker might be willing to change his RegEx from here:
viewtopic.php?p=259401#p259401
to fit for your date (separator) dd.MM.yyyy-format
[.\-]
Re: If var is a date, how?
Posted: 23 Jun 2022, 12:50
by Krd
@BoBo
Many thnaks Bobo
How to avoid current date when the number in var is not a valid date?
Code: Select all
F12::
Myvar := "00123456789"
d := StrSplit(Myvar,".")
t := (StrLen(d.1)=2)
&& (StrLen(d.2)=2)
&& (StrLen(d.3)=4)
? d.3 . d.2 . d.1 . "000000"
: "Nope!"
;if t is digit
; MsgBox % t
;else
; SoundBeep
FormatTime, date,% t, dd.MM.yyyy
MsgBox % res := (date="") ? "Invalid datestamp" : date
return
Re: If var is a date, how?
Posted: 23 Jun 2022, 14:32
by BoBo
Code: Select all
#SIngleInstance, Force
myVar := "00123456789" ; longer than 8 chars, no separator
d := StrSplit(Myvar,".") ; bc of no separator, the array-item d.1 contains the unsplitted content of myVar
MsgBox % d.1 ; as you can see here.
t := (StrLen(d.1)=2) ; fails
&& (StrLen(d.2)=2) ; fails
&& (StrLen(d.3)=4) ; fails
? d.3 . d.2 . d.1 . "000000" ; so this won't happen
: "Nope!" ; but this bc none of the above conditions were true.
MsgBox % t ; as you can see here.
;if t is digit
; MsgBox % t
;else
; SoundBeep
FormatTime, date,% t, dd.MM.yyyy ; so we'd expect that the forced expression will handover 'Nope!' as a corrupt "timestamp" that is supposed to fail...
; AHK Help: "If the date and/or time portion of the timestamp is invalid -- ... -- the date and/or time will be omitted from OutputVar."
; ...but that seems not the case.
MsgBox % if (date="") ? "Invalid timetamp" : date ; ...so I'd expect it to show "Invalid timestamp", instead it shows the current (but converted) date.
return
Weird
Re: If var is a date, how?
Posted: 27 Jun 2022, 09:12
by Krd
Weird
All those MsgBox' confuse me :S
Lol, pardon my pro skills. Im looking for to use this as a true or false condition.
How can I do that? :S Because I would use that true or false in another statement later.
If (Myvar = True )
MsgBox, True
Else, MsgBox, False
As the ocde is now it shows 3 things, which is too much for me
Re: If var is a date, how?
Posted: 28 Jun 2022, 03:09
by gregster
Code: Select all
FormatTime, date,% t, dd.MM.yyyy ; so we'd expect that the forced expression will handover 'Nope!' as a corrupt "timestamp" that is supposed to fail...
; AHK Help: "If the date and/or time portion of the timestamp is invalid -- ... -- the date and/or time will be omitted from OutputVar."
; ...but that seems not the case.
MsgBox % if (date="") ? "Invalid timetamp" : date ; ...so I'd expect it to show "Invalid timestamp", instead it shows the current (but converted) date.
return
Weird
You are putting
t := "Nope!" into the second parameter of
FormatTime. That is considered as "blank" ("Leave this parameter blank to use the current local date and time.") because it has not the format of a
YYYYMMDDHH24MISS timestamp, which can only consist of digits. That's why the part quoted by you doesn't apply here. You ignored: "Otherwise, specify all or the leading part of a timestamp in the YYYYMMDDHH24MISS format." You provided no part of such a timestamp.
You could say, "but 'Nope!' isn't blank", but then I would argue that it is actually not defined what should happen in that case (and depending on undefined behaviour is not a good idea).
Generally, you shouldn't put random letters into the second parameter, as some letters, like
R, are seen as special options:
https://www.autohotkey.com/docs/commands/FormatTime.htm#Additional_Options
As you most likely know, the
if keyword doesn't actually belong into a
ternary (and wouldn't work as a statement in a forced expression of a msgbox) but I guess it is seen as a blank variable named
If here, so it didn't matter.
Re: If var is a date, how?
Posted: 28 Jun 2022, 03:35
by BoBo
@gregster - thx for clarifying this. Regarding FormatTime, I've interpreted it as "put in whatever nonsense, so it'll fail, indicating a wrong time format)", so instead of a(n illegal) string I've set a faulty time format ('00000000000000') and - it works!
Corrected code.
Code: Select all
#SingleInstance, Force
SetBatchLines,-1
myVar := "00123456789" ; longer than 8 chars, no separator
d := StrSplit(Myvar,".") ; bc of no separator, the array-item d.1 contains the unsplitted content of myVar
MsgBox % d.1 ; as you can see here.
t := (StrLen(d.1)=2) ; fails
&& (StrLen(d.2)=2) ; fails
&& (StrLen(d.3)=4) ; fails
? d.3 . d.2 . d.1 . "000000" ; means this won't happen
: 00000000000000 ; but this bc none of the above conditions were true.
MsgBox % t ; as you can see here.
;if t is digit
; MsgBox % t
;else
; SoundBeep
FormatTime, date,% t, dd.MM.yyyy ; so handing over '00000000000000' as a corrupt "timestamp" that is supposed to fail...
MsgBox % (date="") ? "Invalid timetamp" : date ; ...so it now is showing "Invalid timestamp".
return
@Krd - Mission accomplished.
Re: If var is a date, how?
Posted: 30 Jun 2022, 02:44
by Krd
@gregster and
@BoBo
Many thanks to you two! This is a miracle
And thanks for not giving up on me Bobo
Re: If var is a date, how?
Posted: 30 Jun 2022, 04:28
by just me
Code: Select all
#NoEnv
Myvar := "31.02.2020"
If IsValidDate(MyVar)
MsgBox, Bingo!
Else
MsgBox, Bongo!
Exit
IsValidDate(Var) { ; a valid date in dd.MM.yyyy format
; Check whether the whole contents of Var is consisting of 2 digits followed by 1 dot
; followed by 2 digits followed by 1 dot followed by 4 digits
; If so, the digits will be stored in M1, M2, and M3
If RegExMatch(Var, "^(\d{2})\.(\d{2})\.(\d{4})$", M) {
; We assume that M3 contains the year (yyyy), M2 the month (MM), and M1 the day (dd)
; and assemble a date in yyyyMMdd format
Var := M3 . M2 . M1 ; yyyy . MM . dd
; Now we check whether this date is valid
If Var Is Date
Return True
}
Return False
}
Re: If var is a date, how?
Posted: 30 Jun 2022, 06:43
by BoBo
@just me
I'd bet that while I'm writing this line, someone has posted that RegEx already...
Gotcha!
Re: If var is a date, how?
Posted: 30 Jun 2022, 06:55
by Rohwedder
Hallo,
or:
Code: Select all
#NoEnv
Myvar := "31.02.2020"
If IsValidDate(MyVar, "dd.MM.yyyy")
MsgBox, Bingo!
Else
MsgBox, Bongo!
Exit
IsValidDate(Date, Format:="")
{
Date2 := TimeStamp(Date, Format)
IF Date2 is Date
FormatTime, Date2,% Date2,% Format
Return, Date2 = Date
}
TimeStamp(Date, Format:="")
{
Static Noon := A_Year A_MM A_DD 120000, Down := 7060504030201, FOld
FormatTime, D,% 8 Down,% Format?FOld:=Trim(Format):FOld
For all, V in ["Date","D"], Date := Trim(Date) ; add missing leading zeros
%V% := RegExReplace(%V%, "(?<=^|\D)(\d)(?=\D|$)", "0$1")
Loop, Parse,% (0 Down, N := Noon) ; today's noon
IF A_LoopField And Pos := InStr(D, A_LoopField)
N := SubStr(N,1,A_Index-2) SubStr(Date,Pos-1,2) SubStr(N,A_Index+1)
Return, N ; digits >0 from Down in D stand for Date's values
}