If var is a date, how? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

If var is a date, how?

Post by Krd » 21 Jun 2022, 10:05

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 :D
Last edited by Krd on 21 Jun 2022, 10:41, edited 1 time in total.

RussF
Posts: 1264
Joined: 05 Aug 2021, 06:36

Re: If var is a date, how?

Post by RussF » 21 Jun 2022, 10:21

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,

Code: Select all

if my Myvar is a date
should be

Code: Select all

if Myvar is time
per the If Var is Type documentation.

You must provide the command with a date value in the correct format.

Russ

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: If var is a date, how?

Post by Krd » 21 Jun 2022, 10:40

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....

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: If var is a date, how?

Post by BoBo » 21 Jun 2022, 10:49

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

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: If var is a date, how?

Post by Krd » 21 Jun 2022, 10:51

@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.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: If var is a date, how?  Topic is solved

Post by BoBo » 21 Jun 2022, 11:04

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? :think:
That would be noob'ish by nature. I'd bet that while I'm writing this line, someone has posted that RegEx already... :shifty:

Hier dürfte wohl nur eine Änderung des Separators anstehen: viewtopic.php?p=259401#p259401 (fingers crossed)

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: If var is a date, how?

Post by Krd » 21 Jun 2022, 11:42

It has to be this.... Boooring :D

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

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: If var is a date, how?

Post by BoBo » 21 Jun 2022, 11:51

@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.

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: If var is a date, how?

Post by Krd » 21 Jun 2022, 12:29

Not working :D

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: If var is a date, how?

Post by BoBo » 21 Jun 2022, 12:36

@Krd
Well, it does (bc I've tested it meanwhile and silently changed the code) :shh:

Conclusio(n):
"if var is a date? Nope!"
"if var fits a specific date pattern? Yep."

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: If var is a date, how?

Post by BoBo » 21 Jun 2022, 12:51

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 ;)
[.\-] :?:

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: If var is a date, how?

Post by Krd » 23 Jun 2022, 12:50

@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 

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: If var is a date, how?

Post by BoBo » 23 Jun 2022, 14:32

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 :think:

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: If var is a date, how?

Post by Krd » 27 Jun 2022, 09:12


Weird :think:
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 :D

gregster
Posts: 8999
Joined: 30 Sep 2013, 06:48

Re: If var is a date, how?

Post by gregster » 28 Jun 2022, 03:09

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 :think:
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.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: If var is a date, how?

Post by BoBo » 28 Jun 2022, 03:35

@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! :thumbup:

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.

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: If var is a date, how?

Post by Krd » 30 Jun 2022, 02:44

@gregster and @BoBo

Many thanks to you two! This is a miracle :superhappy:

And thanks for not giving up on me Bobo :trollface:

just me
Posts: 9449
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: If var is a date, how?

Post by just me » 30 Jun 2022, 04:28

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
}

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: If var is a date, how?

Post by BoBo » 30 Jun 2022, 06:43

@just me
I'd bet that while I'm writing this line, someone has posted that RegEx already... :shifty:
Gotcha! :mrgreen:

Rohwedder
Posts: 7622
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: If var is a date, how?

Post by Rohwedder » 30 Jun 2022, 06:55

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
}

Post Reply

Return to “Ask for Help (v1)”