Working with Dates - Part 3: Encoding and Decoding Dates
Test and Monitor | Posted January 31, 2007

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

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

Unfortunately, the 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 DaysInYear and 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:


By submitting this form, you agree to our
Terms of Use and Privacy Policy

Thanks for Subscribing

Keep an eye on your inbox for more great content.

Continue Reading

Add a little SmartBear to your life

Stay on top of your Software game with the latest developer tips, best practices and news, delivered straight to your inbox