Automation Testing

QTP DateSerial and Now Function (A Guide to Time Travel)

By Test Guild
  • Share:
Join the Guild for FREE
Code Hacker

Have you ever wished for the ability to time travel? I know I have. While it may not 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 to get past and future dates based on the arguments that you pass it. These functions are handy to know when your QuickTestPro test requires the use of dynamic dates at runtime.

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 function 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 of 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.

QTP Enrollment Enter Dates

 

 

 

 

 

First, to get the 21days prior date, I wrote the following QTP code:

nowMonth = month(now) ‘ This is the current month
nowDay = day(now)’ This is the current day
nowYear = year(now)’ This is the current year

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

startDate = DateSerial(nowYear,nowMonth,nowDay-21)

‘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.

Do
nowMonth = month(startDate)
nowDay = day(startDate)
nowYear = year(startDate)

dayDate = DateSerial(nowYear,nowMonth,nowDay-1)
thisDay = WeekDay(dayDate)
startDate = dayDate

Loop until thisDay = "2" ' 2 = Monday

After the loop the startDate equals 01/10/2011 which is the Monday date.

QTP Enrollment Calendar
Enrollment Calendar

 

 

 

 

 

 

 

 

 

 

Next to get the end date I pass the DateSerial function the start date value and add 61 days to it

endDate = DateSerial(nowYear,nowMonth,startDate+61)'Future date

Once I have the values I enter them in their corresponding fields:

TextBoxAW "txtStudyStartDate","ENTER",startDate,""
TextBoxAW "txtStudyEndDate","ENTER",endDate,""

QTP Enrollment Dates Filed Out

 

 

 

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.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

What is Synthetic Monitoring? (2024 Guide)

Posted on 04/18/2024

I've found over the years many testers are unsure about what is synthetic ...

SafeTest: Next Generation Testing Framework from Netflix

Posted on 03/26/2024

When you think of Netflix the last thing you probably think about is ...

Top Free Automation Tools for Testing Desktop Applications (2024)

Posted on 03/24/2024

While many testers only focus on browser automation there is still a need ...

Discover Why Service Virtualization is a Game-Changer:  Register Now!