Have you ever wished for the ability to time travel? I know I have. While it may not be ever be truly possible, QTP’s DateSerial() and Now() functions get me as close to it as I think I’ll ever be. The DateSerial function allows you get past and future dates based on the arguments that you pass it. These functions are handy to know when your Quicktest Pro test requires the use of dynamic dates at run time.
DateSerial,DateAdd and Now functions parameters:
- DateSerial (year, month, day) – returns a date for a specified day month and year.
- Now() – returns the current date and time according to the settings of your computers system’s date and time.
- DateAdd(interval,number, date) - This functions allows you to add or subtract a specified time interval from a date.
Now() example
In QTP enter msgbox Now() it will return the date/time similar to this format: mm/dd/yyy h:mm:ss AM/PM (If you don't need to current time you can use the Date() function instead.)
If you wanted to take today's day and add 1 day to it. For example if today was 08/02/2011 and you wanted to add 1 day to it to make it 08/03/2011:
todayDate = now()
1DayAhead = DateAdd("d",1,todayDate)
QTP Real World Example:
For a recent test, I needed to create a clinical trial study with a start date 21 days in the past from the script’s runtime date. I also needed to know what the date of the Monday that fell in that time frame was. Finally, I also needed to enter an end date that was 61 days in the future from the start date. To accomplish all this, I used the Now and DateSerial functions.
First, to get the 21days prior date, I wrote the following QTP code:
This will return a day that is 21 days in the past. If I ran the script on 02/01/2011 it will return the startDate of 01/01/2011
‘Next I need to get the date for the Monday in the returned startDate week. ‘This will loop until the current date in the startDate equals a Monday date.
After the loop the startDate equals 01/10/2011 which is the Monday date.
Next to get the end date I pass the DateSerial function the start date value and add 61 days to it
Once I have the values I enter them in their corresponding fields:
Pretty cool, huh? When I first got the requirements from the manual tester, he thought it would be impossible to automate this functionality. But thanks to QTP’s Now and DateSerial function, dynamic dates can be created with ease.




{ 5 comments… read them below or add one }
Thanks sooooo much… Above content is really useful!!
Pooja » Cool! Thanks Pooja!
I need to replace the “/” and “:” from the NOW function. Am using NOW to append date and time to an output file in a script. Cannot save fle with the normal output format from NOW function. Am new to QTP and have not had success with the replace function.
Vera Z » Hi Vera can you send me the code that you are using? Also what values are you trying to use to replace the / and : with> I dbl checked and I was able to do the following without issue:
mydate = Now
msgbox mydate
mydate = replace(mydate,”/”,”^”)
mydate = replace(mydate,”:”,”{“)
msgbox mydate
Hi Joe. Thanks for the advice. I got the code to work. It appears at the end of this message. [I did not have the correct format in the replace statement]
DateTimeStamp = Now
DateTimeStamp = replace(DateTimeStamp,”/”,”-”)
DateTimeStamp = replace(DateTimeStamp,”:”,”-”)
DataTable.ExportSheet “c:\QTP Tests\Admin_Companies_” & DataTable.Value(“Agency”, dtGlobalSheet) & DateTimeStamp & “.xls”, 1
{ 1 trackback }