Automation Testing

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

By Test Guild
  • Share:
Join the Guild for FREE

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.

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

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

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

  4. Hi
    I’ve issue in comparing date with current date and displayed date in my application.
    In my application date is displayed as 01/02/2014
    And date retrieved through QTP (date function) is displayed as 1/2/2014.
    When I compared it, my result fails. Please suggest me, how to overcome this issue?

    Example:
    Applicationdate=”01/02/2014”
    Currentdate= Date
    If Applicationdate= Currentdate Then
    Msgbox “Date Passed”
    Else
    Msgbox “Date Failed”
    End If

    Month displayed in application is 02 while month displayed by qtp date function is 2.

    Thank’s
    Jagadeesh Mani
    visitjaga@gmail.com

  5. Hi
    I’ve issue in comparing date with current date and displayed date in my application.
    In my application date is displayed as 01/02/2014
    And date retrieved through QTP (date function) is displayed as 1/2/2014. When I compared it, my result fails. Please suggest me, how to overcome this issue?
    Example:
    Applicationdate=”01/02/2014”
    Currentdate= Date
    If Applicationdate= Currentdate Then
    Msgbox “Date Passed”
    Else
    Msgbox “Date Failed”
    End If

    Month displayed in application is 02 while month displayed by qtp date function is 2.
    Thank’s
    Jagadeesh Mani
    visitjaga@gmail.com

  6. Help my to write my date and the real time like this format :
    Browser(“Browser”).Page(“Page”).WebEdit(“agenda”).Set “12/05/04 16:40”

    What the script to write “12/05/04 16:40”? Please;
    Thanks.

    Paul

  7. Hi Joe,

    I have an issue when calculating the duration. For instance i have added a code like this

    starttime = Time()

    perform some actions

    endtime = Time()

    difference = endtime – starttime

    Desired Output must be – hh :mm:ss

    Coluld you please provide a solution.

Comments are closed.

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

Symbolic AI vs. Gen AI: The Dynamic Duo in Test Automation

Posted on 09/23/2024

You've probably been having conversations lately about whether to use AI for testing. ...

8 Special Ops Principles for Automation Testing

Posted on 08/01/2024

I recently had a conversation, with Alex “ZAP” Chernyak about his journey to ...

Top 8 Open Source DevOps Tools for Quality 2024

Posted on 07/30/2024

Having a robust Continuous Integration and Continuous Deployment (CI/CD) pipeline is crucial. Open ...