≡ Menu
Joe Colantonio – Selenium-UFT-QTP-SoapUI-ALM-LoadRunner & more

QTP – DateSerial and Now Function — A QTP Guide to Time Travel

QTP – DateSerial and Now Function — A QTP Guide to Time Travel post image

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.

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<br /> nowDay = day(now)’ This is the current day<br /> 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<br /> nowMonth = month(startDate)<br /> nowDay = day(startDate)<br /> nowYear = year(startDate)

dayDate = DateSerial(nowYear,nowMonth,nowDay-1)<br /> thisDay = WeekDay(dayDate)<br /> 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,""<br /> 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.

7 comments… add one

  • Pooja

    Thanks sooooo much… Above content is really useful!! :)

    • Joe Colantonio

      Pooja » Cool! Thanks Pooja!

  • Vera Z

    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.

    • Joe Colantonio

      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

      • Vera Z

        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

  • Jagadeesh

    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

  • Jagadeesh

    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

Leave a Comment

css.php