Working with dates. Part 1: Getting Tomorrow's Date
When writing scripts we often deal with dates. There are certain date value formats and TestComplete routines that help handle dates.
TestComplete's scripting engine supports OLE-compatible data types, the date-time values are implemented as floating-point variant values in a special format. The integer part of this value represents the number of days that have passed since December 30, 1899. The number after the decimal separator represents the fraction of a 24-hour period that has elapsed. Below are some examples of date-time values and their meaning:
|0.25||December 30, 1899. 6:00 AM|
|36345.5||July 4, 1999. 12:00 PM|
|39094.65625||January 12, 1899. 3:45 PM|
When you are only working with date values the fractional part can be omitted.
TestComplete has the
Utilities object that contains methods and properties that can be useful when operating with dates.
|DateSeparator||Specifies the character separating year, month and day in date values.|
|LongDateFormat||Specifies the format string used to convert a date value to a string.|
|ShortDateFormat||Specifies the format string that is used to convert a date value to a short string.|
|TwoDigitYearCenturyWindow||Specifies the integer value used to convert strings to date and time values.|
|Date||Returns the current date set on your computer.|
|DateTimeToFileDate||Converts a variant value that stores date and time to an integer value.|
|DateTimeToStr||Converts the variant value that stores date and time to a string.|
|DateTimeToString||Converts a variant value storing date and time to a string using the specified format string. (For VBScript and DelphiScript only.)|
|DateTimeToSystemTime||Converts a variant value that holds a date and time, to a |
TSystemTime value. (For VBScript and DelphiScript only.)
|DateToStr||Converts a variant value holding a date to a string.|
|DayOfWeek||Returns the day of the week for the specified date.|
|DecodeDate||Splits the specified date value into the year, month and day of the month. (For VBScript and DelphiScript only.)" |
|EncodeDate||Converts the date specified by year, month and day to a variant value.|
|FileDateToDateTime||Converts an integer value that stores the timestamp of a file to a variant value storing the date and time.|
|FormatDateTime||Converts the given date-time value to a string using the specified format.|
|IncMonth||Increments the given date value by the specified number of months.|
|IsLeapYear||Determines whether the given year is a leap year.|
|Now||Returns the current date and time.|
|ReplaceDate||Changes the date part of the first parameter into the new date that is specified as the second parameter. (For VBScript and DelphiScript only.)|
|StrToDate||Converts the specified string to a value that encodes the date.|
|StrToDateTime||Converts the specified string to a value that encodes the date and time.|
|SystemTimeToDateTime||Converts a |
TSystemTime value to a variant value that encodes the date and time.
Utilities object is accessible if the Utilities plug-in is installed (by default it is). The plug-in file (tcUtilities.pls) is supplied with TestComplete and located in the Extensions folder. The object is available for all supported scripting languages, so that you can use it to operate with date values regardless of the chosen language. The name of the
Utilities object is not mandatory, so the
Now() method calls are both valid.
The samples below demonstrates how this object can be applied to calculate the tomorrow's date. The current date is obtained via the
Utilities.Date() method. Then the current date value is treated as number and is incremented by 1. Another method of the Utilities object is used to convert the date value into a string that is posted to TestComplete's log.