WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: tsgjersey on August 11, 2022, 07:44:36 AM

Title: Validate a date
Post by: tsgjersey on August 11, 2022, 07:44:36 AM
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
Title: Re: Validate a date
Post by: td on August 11, 2022, 08:13:18 AM
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.
Title: Re: Validate a date
Post by: kdmoyers on August 11, 2022, 08:33:47 AM
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)
Title: Re: Validate a date
Post by: tsgjersey on August 11, 2022, 10:22:12 AM
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
Title: Re: Validate a date
Post by: td on August 11, 2022, 11:04:08 AM
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.

Title: Re: Validate a date
Post by: td on August 12, 2022, 01:33:32 PM
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...