This is Part6 of QTP and DotNetFactory series by Saket. Refer Part1, Part2,Part3, Part4, Part5
In the first part of this series you have already seen an example to manipulate the date in the required format. In this part we will see how to use DateTime properties and methods to work easily using DotNetFactory in QTP.
The DateTIme structure represents an instance in time and can be expressed as a particular date and time of the day. It can also be used for various operations on date/time variables.
We use ‘System.DateTime’ as type name. We do not need to specify the assembly for this as it belongs to the already loaded namespace ‘System’ (mscorlib.dll).
Set MyDate = DotNetFactory.CreateInstance("System.DateTime")
Let us start with a very simple example to get the number of days in a month.
To get the number of days in a month we have method DaysInMonth,. To find the number of days for November 2009 we can use
MyDate.DaysInMonth(2009,10)
DaysInMonth method accepts two arguments year and month.
Example 18
Set MyDate = DotNetFactory.CreateInstance("System.DateTime") msgbox "Number of Days in November 2009: " & MyDate.DaysInMonth(2009,11) Set MyDate = Nothing
This gives the output as ‘30’.
This was so simple and you may say that it is also possible in QTP with simple statements. Consider a case where you need to change the format of the Date. You have a date value in say MM-DD/YYYY (e.g. 10-22/2009) format and you want to convert it into DD/MM/YYYY (e.g. 22/10/2009)
When you will try to do this you will definitely end up with a huge complex function, something like the example below.
Example 19
Public Function dateManipulate(sDate) locDelimOne = InStr(1, sDate, "-") locDelimTwo = InStr(1, sDate, "/") If locDelimOne = 2 Then sMonth = Left(sDate, 1) sMonth = "0" & sMonth ElseIf locDelimOne = 3 Then sMonth = Left(sDate, 2) End If sDay = Mid(sDate, locDelimOne + 1, locDelimTwo - locDelimOne - 1) If Len(sDay) = 1 Then sDay = "0" & sDay End If sLen = Len(sDate) – locDelimTwo sYear = Mid(sDate, locDelimTwo + 3, sLen) iDate = sMonth & "/" & sDay & "/" & sYear dateManipulate = iDate End Function msgbox DateManipulate("10-22/2009")
When you will run the above script you will get the required output. But you will see using DotNetFactory, we can get the required output in just three simple statements. See the example below.
Example 20
Function DateManipulate(sDate) Set MyDate = DotNetFactory.CreateInstance("System.DateTime") Set oDate = MyDate.Parse(sDate) DateManipulate = oDate.ToString("dd/MM/yyyy") Set MyDate = Nothing End Function msgbox DateManipulate("10-22/2009")
Gives output as
In the above example using DotNetFactory we have retrieved the instance of System.DateTime and we are using Parse method to convert to its DateTime equivalent. And using ToString Method we are converting the date into the required format.
Also you will notice that the function that you have created using DotNetFactory is capable to convert from any format to the DD/MM/YYYY format, where the earlier normal function is capable to convert from one format only, you will need to create more function for different formatting option.
You can try these
msgbox DateManipulate(“10/21/2009”)
msgbox DateManipulate(“10.21.2009”)
msgbox DateManipulate(“2009-10-23”)
Date Time structure includes other methods as well which directly converts into a particular method.
ToLongDateString – Converts the value of this instance to its equivalent long date string.
ToShortDateString– Converts the value of this instance to its equivalent short date string
ToLongTimeString – Converts the value of this instance to its equivalent long time string
ToShortTimeString – Converts the value of this instance to its equivalent short time string
Example 21
Set MyDate = DotNetFactory.CreateInstance("System.DateTime") Set oDate = MyDate.Parse("10/22/2009 01:30:50") msgbox oDate.ToLongDateString ’ returns - Thursday, October 22, 2009 msgbox oDate.ToLongTimeString ‘ returns - 1:30:50 AM msgbox oDate.ToShortDateString ‘ returns - 10/22/2009 msgbox oDate.ToShortTimeString ‘ returns - 1:30 AM Set MyDate = Nothing
Let us now see more examples of using DotNetFactory for Date/Time operation in QTP scripts.
Add Days, Months and Years
The most common operation with Date is Adding/Subtracting Date values. DateTime structure includes direct methods of doing such operation
AddDays – Adds the specified number of days to the value of the instance.
AddMonths – Adds the specified number of months to the value of the instance.
AddYears – Adds the specified number of years to the value of the instance.
Example 22
Set MyDate = DotNetFactory.CreateInstance("System.DateTime") Set rightNow = MyDate.Now Print "Current Date: " & rightNow.ToString Print "Add Days - 1: " & rightNow.AddDays(1) Print "Add Months - 1: " & rightNow.AddMonths(1) Print "Add Years - 1: " & rightNow.AddYears(1) Set rightNow = Nothing Set MyDate = Nothing
Output
Add Hours, Minutes, Seconds and Milliseconds
Similar to AddDays, AddMonths, AddYears, we have AddHours, AddMinutes, Add Seconds, and AddMilliSeconds.
Example 23
Set MyDate = DotNetFactory.CreateInstance("System.DateTime") Set rightNow = MyDate.Now Print "Current Time: " & rightNow.ToString Print "Add Hours - 1: " & rightNow.AddHours(1) Print "Add Minutes - 1: " & rightNow.AddMinutes(1) Print "Add Seconds - 1: " & rightNow.AddSeconds(7) Print "Add MilliSeconds - 1: " & rightNow.AddMilliSeconds(1000) Set rightNow = Nothing Set MyDate = Nothing
Output
Test Leap Year
We can use another method ‘ISLeapYear’ to determine whether a year is a leap year or not.
This method returns True if the specified year is a leap year otherwise returns False
Example 24
Set MyDate = DotNetFactory.CreateInstance("System.DateTime") sYear = 2010 If MyDate.IsLeapYear(sYear) Then msgbox sYear & " is Leap Year" else msgbox sYear & " is not Leap Year" End If Set MyDate = Nothing
Output
Convert to Universal Time and Local Time
DateTime structure includes methods to convert the times to universal time from local time and vice versa.
ToUniversalTime – converts the value of Date time object to coordinated universal time.
ToLocalTime – converts the value of Date time object to local time
Example 25
Set MyDate = DotNetFactory.CreateInstance("System.DateTime") Set sDate = MyDate.Now Msgbox "Local Time is : " & sDate.ToString & vbcrlf & "Equivalent Universal Time is : " & sDate.ToUniversalTime.ToString Set sDate = MyDate.Parse("11/2/2009 9:24:23 AM") Msgbox "Universal Time is : " & sDate.ToString & vbcrlf & "Equivalent Local Time is : " & sDate.ToLocalTime.ToString Set sDate = Nothing Set MyDate = Nothing
Output
In the above examples we have used ‘Set MyDate = DotNetFactory.CreateInstance(“System.DateTime”)’ to create the instance of DateTime object. There are other direct methods of creating this object to particular date time by passing arguments.
Set MyDate = DotNetFactory.CreateInstance("System.DateTime",,2009,11,2) msgbox MyDate.ToString
This will return
Set MyDate = DotNetFactory.CreateInstance("System.DateTime",,2009,11,2,9,45,50) msgbox MyDate.ToString
This will return
If you wish to set the current Date and time…
Set MyDate = DotNetFactory.CreateInstance("System.DateTime") Set sDate = MyDate.Now
instead of using the code above, we can directly use
Set MyDate = DotNetFactory.CreateInstance("System.DateTime").Now
Want to share your knowledge with the thousands of LearnQTP readers? Contact Us
Hi, how to use hashset or sortedset using dotnetfactory? Arraylist allows duplicate values. I want only unique values. How I can achieve it by using .net framework collections?
I tried it.. its coool.. Thanks stv..
to answer my own question of yesterday, I found a “DotNet Assembly Viewer” here: http://www.codeproject.com/KB/dotnet/asmex.aspx
I have not tried it out yet.
Hi stv_kp
there is no other way to get the list of all loaded assembly except using a tool or execute some code in .net. I recommed you to refer MSDN.
This is a good tutorial. But how do I discover what Assemblies are loaded, for me to use? Besides the ones you have mentioned (System.DateTime and System.Windows.Forms) there must be many others. Is this in the registry somewhere? Or could I run a short QTP script to list them all?
Hi,
Thanks for posting useful tutorials …
I have a date in the format 10/24/2009 and I want to convert it to “24-Oct-2009”. Is there any inbuilt function in QTP 9.5 for that?
Hi,
This is an excellent site that i have ever seen.
So many new things in a very simple way.
Mani.
pls Refer MSDN
Greate ankur. Its very useful will save lot of time especially during date and time validation.
Very useful one.It will save lot of time for Date time validations.keep Up the good work.
btw Do we have any HP QTP Documentation which talks about all the available methods for DotNetFactory
Interesting use of the DotNetFactory in QTP.
Is it possible to use the DotNetFactory in QTP to access properties of objects that are already instanced within a .NET application?
i.e. Is there a ReferenceInstance function that we can use alongside the CreateInstance one (which has been a great read in the past 6 parts)?