Actually, today's lecture consists of two questions:
- How to get system time and date?
- How to set system time and date?
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.
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 :)
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:
- QTP - How to set/get system time and date?
- How to execute QTP script from LoadRunner?
- QTP - How to get font size/color, background color and other attributes of controls
- How to minimize/maximize QTP window before the QTP script execution?
9 comments:
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
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 :)
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...
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'.
Oh, right. I was confusing this with LR parameterization symbols.
Sorry for that :)
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.
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.
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
@ Anonymous,
I used:
curDateTime=Now
curDate=Date
curTime=Time
just to simplify a date formatting.
Post a Comment