QTP - How to set/get system time and date?

QTP - How to set/get system time and date?

QTP allows running different types of test. And sometimes, it needs perform time and date manipulations on a computer. In this article, I will show and explain - how to get system time/date and change them.

Actually, today's lecture consists of two questions:
  1. How to get system time and date?
  2. How to set system time and date?
Let start training right now :)
So, the first section is:


How to get system time and date?


Actually, this is not difficult task! Use the following functions:
  • Now - function returns the current date and time according to the setting of your computer's system date and time.
  • Date - function returns the current system date.
  • Time - function returns a Variant of subtype Date indicating the current system time.
So, the code is very simple. I provide it with a screen shot containing results:


Well, let's deal with the second section:


How to set system time and date?

For that, I will use simple DOS commands:
  • time - this command displays or sets the system time:
    To change the time, pass it as a parameter to time command. See the above screen shot as an example.

  • date - this command displays or sets the system date:
    To change the date, pass it as a parameter to date command. See the above screen shot as an example for date manipulations :)
Now, I will combine all the above VBScript functions (Now, Date, Time) and DOC commands (time and date):

As you can see, initial date and time were '26.10.2007 22:45:10'. Then I added 3 days and 2 hours to initial date & time. The result of addition is - '30.10.2007 0:45:10'.

By the way, could you guess, why the result day is 30th, not 29th? :) To answer, please pay attention that we added 2 hours too :)

The last step - running of time and date commands from command line. I used Run method of WScript.Shell object.


So, as you can see - date and time manipulation is easy enough.




Related articles:

9 comments:

Anshoo Arora said...

You have posted:

newDateTime = DateAdd("y",3,newDateTime) 'add 3 days

and the output has shown an addition of 3 days (or 4?). Isn't it supposed to be:

newDateTime = DateAdd("d",3,newDateTime) 'add 3 days

Dmitry Motevich said...

2Anshoo:
Please, read the article carefully.
There is no any error :)

Please, read my question:
By the way, could you guess, why the result day is 30th, not 29th? :) To answer, please pay attention that we added 2 hours too :)

I hope, It will help you explain the logic :)

Anshoo Arora said...

You are absolutely right.

However, I was wondering why using 'y' gave you an output of +3 days instead of +3 years. Shouldn't 'd' give you that output: x=y('d',3,x)?

Is it because of the difference in LR versions? Was just curious...

Dmitry Motevich said...

2Anshoo:
'y' means 'Day of year', not 'year' :)

Please, see Help of DateAdd VBScript function

If you want to add several years, just specify 'yyyy'.

Anshoo Arora said...

Oh, right. I was confusing this with LR parameterization symbols.

Sorry for that :)

Unknown said...

I'm a complete noob, but is there a way to make two scripts? One that sets the date to some pre-defined date (say 2 days in advance), and then another that returns it to what it "SHOULD" be? Imagine it gets complicated when it runs into the condition that the day naturally advances between me setting and resetting. Looking for a quick way to change my system time/date a lot for testing a calendar app, but its screwing up my email send dates, so need quick way to switch back. Running Vista if thats a consideration.

Dmitry Motevich said...

2 Mondo:
Sure, you can switch a time back.
If you use the following code to *add* 2 hours:
DateAdd("h", 2, curDateTime)

then you have to *subtract* 2 hours:
DateAdd("h", -2, curDateTime)

After that, your time will revert to correct value.

Anonymous said...

You have mentioned
curDateTime=Now
curDate=Date
curTime=Time

but why it cant be
curDateTime=DateTime
curDate=Date
curTime=Time

or
curDateTime=Now
curDate=Now
curTime=Now

Please explain.
Thanks in advance,
sricharantester@gmail.com

Dmitry Motevich said...

@ Anonymous,
I used:
curDateTime=Now
curDate=Date
curTime=Time
just to simplify a date formatting.