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
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
Code: Select all
if my Myvar is a date
Code: Select all
if Myvar is time
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
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
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
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
All those MsgBox' confuse me :S
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.WeirdCode: 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
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
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
}
Gotcha!I'd bet that while I'm writing this line, someone has posted that RegEx already...
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
}