Working with Dates - Part 3: Encoding and Decoding Dates
As I wrote in the first post of this series, TestComplete stores data values in a special format. Because of this, special routines are required that convert the variant value to a calendar date format and back. These operations are performed by the
DecodeDate methods of the
Utilities object. These methods take into account whether the current year is a leap year and the number of days in a month so that the resulting value is guaranteed to be valid. The
EncodeDate accepts the date parts and returns the date value as a variant.
DecodeDate method performs the opposite operation: it takes a variant date and splits it into date parts that are returned via the parameters. Here is an example of how to use this routine:
DecodeDate method is not useful in JScript, since it returns the resulting year, month and day via the parameters which is not supported in JScript. To solve this problem you can use the following function. It has two parameters: the first one specifies the date to be parsed and the second defines which part of date the function will return. The acceptable values for the second parameter are: "Y" - returns the year that corresponds to the input date, "M" - returns the month number and "D" - returns the day of month that corresponds to the input date. If the second parameter is omitted then the function returns the string that holds the complete date in the "DD/MM/YYYY" format. This function uses two helper functions
DaysInMonth that were given in a previous post.
This function will be used in later JScript, C#Script or C++Script samples when date parts are required. Here is an example of how to use this routine: