Hi, we have just purchased WinBatch an am unsure how to check if a passed-in parameter %param1% is a valid date.
I am using isInt to validate if a parameter is an integer but cannot find an equivalent for a date.
Thanks
Dates can be represented in many ways. For example, a date could be a Julian date, Windows file time, UNIX time, or text representing a locale-formated string representing a human-readable date or date/time. All of these formats occur on the Windows OS.
So the first step is to decide on accepted date formats.
Deciding which format you want is often dictated by what function you are planning to use. For example, if you want to use the WinBatch TimeAdd then you are going to need to use the format YYYY:MM:DD:hh:mm:ss format.
a = "2020:08:11:09:52:00"
b = TimeAdd(a,"0000:00:00:01:00:00")
message(a,b)
Thanks TD and KDMoyers
The date will always be in "dd/mm/yyyy" format.
In pseudo-code I am trying to do the following:
myDate = "42/12/1931"
If isDate( myDate ) = FALSE THEN
Display Error Message
End IF
One way is to use the ObjectType function:
vtDate = ObjectType("DATE","42/12/1931")
It generates a 1702 "Invalid value" error if the date is not valid.
You can use your Consolidated WIL Help file to investigate the ErrorMode function and figure out how to easily trap 1000 range errors.
A simple example. As always bugs are provided at no extra charge.
;; Test if a string contains a valid date
;; _Date - string to test
;; Returns - 1 if valid; otherwise, 0
#DefineFunction IsDate( _Date )
Prev = ErrorMode(@off)
ObjectType("DATE",_Date)
nError = LastError()
ErrorMode(Prev)
return nError != 1703
#EndFunction
;; Test
Date = "42/12/1931"
if isDate(Date) then Text = "Yes"
else Text = "No"
Message("Is ":Date:" Valid?", Text)
exit
Note that the error is 1703 and not 1702 as previously stated...