Validate a date

Started by tsgjersey, August 11, 2022, 07:44:36 AM

Previous topic - Next topic

tsgjersey

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

td

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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

kdmoyers

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.
Code (winbatch) Select
a = "2020:08:11:09:52:00"
b = TimeAdd(a,"0000:00:00:01:00:00")
message(a,b)
The mind is everything; What you think, you become.

tsgjersey

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

td

One way is to use the ObjectType function:

Code (winbatch) Select
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.

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

A simple example. As always bugs are provided at no extra charge.

Code (winbatch) Select
;; 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...
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade