QTP DateSerial and Now Function (A Guide to Time Travel)
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.
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.

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,""
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.
Joe Colantonio is the founder of TestGuild, an industry-leading platform for automation testing and software testing tools. With over 25 years of hands-on experience, he has worked with top enterprise companies, helped develop early test automation tools and frameworks, and runs the largest online automation testing conference, Automation Guild.
Joe is also the author of Automation Awesomeness: 260 Actionable Affirmations To Improve Your QA & Automation Testing Skills and the host of the TestGuild podcast, which he has released weekly since 2014, making it the longest-running podcast dedicated to automation testing. Over the years, he has interviewed top thought leaders in DevOps, AI-driven test automation, and software quality, shaping the conversation in the industry.
With a reach of over 400,000 across his YouTube channel, LinkedIn, email list, and other social channels, Joe’s insights impact thousands of testers and engineers worldwide.
He has worked with some of the top companies in software testing and automation, including Tricentis, Keysight, Applitools, and BrowserStack, as sponsors and partners, helping them connect with the right audience in the automation testing space.
Follow him on LinkedIn or check out more at TestGuild.com.
Related Posts
If you’re still writing Playwright scripts by hand in 2026, you’re solving yesterday’s problem. I talked to Karim Jouini, CEO […]
The Bottom Line for 2026: After 25+ years in QA and interviewing over 580 automation experts on the TestGuild podcast, […]
Mailinator How to Test Email and SMS Workflows (Product Spotlight) Mailinator is a disposable inbox platform built for developers and […]
Look, I’ve been doing test automation for over 25 years. I’ve heard the predictions. “Manual testing is dead.” “AI will […]





