Tricentis Tosca 16.0 Released on Feb-2023 ----- UFT has been upgraded from UFT 15.0.1 to UFT One 15.0.2, Beginning at November 2020.

Monday, 18 May 2015

Date & Time Functions in Vb-Script

List of Date Functions available in vb-script:
  1. Date
  2. Time
  3. Now
  4. Month
  5. MonthName
  6. Weekday
  7. Weekdayname
  8. Year
  9. Hour
  10. Minute
  11. Second
  12. Dateadd
  13. DateDiff
Uses of Date Functions: Assume that you are automating the application in which one of the screen date field accepts the different formats or need to add required days/months before entering the value in the date field. One more issue with day and month inputs when valid format was MM or DD. If we read the month and day from the system it returns a single digit month or day, but the application does not accept a month 6 (June) instead requires 06. In this kind of situations we require this date functions. 

Now : Returns the current system date and time.
Sysdate = Now
msgbox Sysdate
Result: UFT shows current system date and time in a dialog box

Date : Returns the current system date.
Mydate = Date
msgbox Mydate


Result: UFT shows current system date  in a dialog box

Day : Returns the current day from the system date.
Sysdate = Now
MyDay = Day (Sysdate)
msgbox MyDay


Result: UFT shows current day from the system date in a dialog box.

Example-1:

MyDay=Day(“January 122009”)
Msgbox MyDay


Output: 12

WeekDay : Returns the weekday from the given date.

Sysdate=Now
MyWeekDay=WeekDay(Sysdate)
msgbox MyWeekDay

Result: Returns(it returns the integer)  the weekday from the given date. 

Note: Sunday – 1, Monday – 2,Tuesday–3, Wednesday – 4, Thursday – 5, Friday – 6, Saturday – 7. 

Example:

MyDate=#January 12, 2009#      'Assgin a date
MyWeekDay=WeekDay(MyDate)
Msgbox  MyWeekDay

Output: 1 for Sunday, 2 for Monday, …so on

WeekDayName: Returns the weekday name for the specified day of the week.

Sysdate=Now
MyWeekDay=WeekDay(Sysdate)
Msgbox MyWeekDay
MyWeekDayName=WeekDayName(MyWeekDay)
msgbox MyWeekDayName

Result: Returns the weekday name for the specified day of the week.

Syntax: WeekdayName(weekday[,abbreviate[,firstdayofweek]])

Weekday: it Required the number of the weekday.
Abbreviate: it is an Optional. it gives Boolean value that indicates if the weekday name is to be abbreviated.
Firstdayofweek: it is an optional. it Specifies the first day of the week, it means that Firstdayofweek(Optional) has values like 0,1, 2... vb Use System DayOfWeek, 1 for Sunday, 2 for Monday and so on.

Msgbox(WeekdayName(4))
Output: Wednesday

Msgbox(WeekdayName(weekday(Date)))
Output: Sunday(obviously if today is Sunday)

Year: Returns the current year from the system date.
Sysdate = Now
MyYear = Year(Sysdate)
msgbox MyYear

Result: UFT shows current year from the system date in a dialog box.

Example:
MyDate=#January 12, 2009#             'Assign a date
MyYear=Year(MyDate)
Msgbox MyYear

Output: 2009

Month: Returns the current month from the system date.
Sysdate = Now
MyMonth = Month (Sysdate)
msgbox MyMonth

Result: UFT shows current month from the system date in a dialog box.

Example:

MyYear=Month(Now)
Msgbox MyYear


Output: 1 for January

MonthName: The MonthName function returns a string indicating the specified month.

Syntax: MonthName(month[, abbreviate])

Msgbox(MonthName(9))
Output:September

Month: it Required, and it specifies the number of the month(January is 1, februry is 2 etc)
Abbreviate: it is an optional. A Boolean value indicates that if the month name is to be abbreviated. Default is False.

DateAdd: it returns a date after adding a specified time interval.


Syntax: DateAdd(interval, number, date)


Msgbox(DateAdd("m"1"31-Mar-09"))
Output: 4/30/2009

Msgbox(DateAdd("m",-1,"31-Mar-09"))
Output:2/28/2009

Note: Interval can be YYYY-Year, q-Quarter, m-Month, h-hour, n-Minute

DateDiff: The DateDiff function returns the number of intervals between two dates.


Syntax: DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])

Msgbox(DateDiff("m",Date"12/31/2009"))
or
Msgbox(DateDiff("m",03/14/2009"12/31/2009"))
Output:9

Note: Interval can be YYYY-Year, q-Quarter, m-Month, h-hour, n-Minute

Firstdayofweek(Optional): it has values like 0 for vbUseSystemDayOfWeek, 1 for vbSunday, 2 for vbMonday so on
FirstWeekofyear(optional):  0 for vaUseSystem, 1 for vbFirstJan 1, i.e Start with the week in which January 1 occurs(default), 2 for vbFirstFourDays i.e Start with the week that has at least four days in the new year, 3 for vbFirstFullWeek i.e Start with the first full of the new year.

DatePart: DatePart function returns the specified part of a given date. 

Syntax: DatePart(interval, date[,firstdayof week[,firstweekofyear]])

Msgbox(DatePart("d"Date))
Result : Returns the specified part of a given date
Or
Msgbox(DatePart("d""3/14/2009"))

Example:
Mypart = DatePart("d",now)msgbox Mypart       'Here getting the day part of the current date

Result : Returns the specified part of a given date

Note:Interval can be YYYY- Year, q-Quarter,m-month ,d-day


DateSerial: The dateserial returns a Date value representing a specified year, month, and day, with the time information set to midnight(00:00:00)

Syntax: DateSerial(Year, month, day)


Msgbox(DateSerial(2009,11,29))
Output: 11/29/2009

Msgbox(DateSerial(2009-9,9-2,1-1))
Output: 6/30/2000

DateValue: The DateValue function returns a type Date.

Syntax: DateValue(date)

Msgbox(DateValue("31-jan-09"))
Ouiput:1/31/2009

IsDate:  IsDate returns True if the expression is a date or can be converted to a valid date, Otherwise it  returns False.

Syntax: IsDate(Date)


MyDate= "January 12, 2009"
YesDate=#01/12/2009#
NoDate="Yes Boss"
MyCheck=IsDate(MyDate) 'Returns True
MyCheck=IsDate(YesDate) 'Returns True
MyCheck=IsDate(NoDate) 'Returns False

CDate:  The CDate function converts a valid date and time expression to type Date. Use the CDate function to determine if date can be converted to a date or time. The CDate function uses local setting to determine if a string can be converted to a date.

The  following example example uses the CDate function to convert a string to a date.

X="January 12, 2009"
If IsDate(x) then
   Msgbox (CDate(x))
End if
Output: 1/12/2009

Timer Functions

Timer:  The Timer function returns the number of seconds that have passed since midnight(12:00:00 AM).

Msgbox(Timer)
Output:  86296.13 at 11:58:16 PM

Minute : Return current minutes from the system date.

Sysdate = Now
MyMinute = Minute (Sysdate)
msgbox MyMinute
Result: UFT shows current minutes from the system date in a dialog box.

Second : Return current seconds from the system date.

Sysdate = Now
MySecond = Second (Sysdate)
msgbox MySecond
Result: UFT shows current seconds from the system date in a dialog box

Hour : Return current hour from the system date.

Sysdate = Now
MyHour = Hour (Sysdate)
msgbox MyHour
Result: UFT shows current hour from the system date in a dialog box

Time : Returns the current system time.

Mytime = Time
msgbox Mytime
Result: UFT shows current system time in a dialog box

TimeSerial: The TimeSerial function returns the  time for a specific hour, minute, and second.


Syntax: TimeSerial(hour, minute, second)

Msgbox(TimeSerial(0,18,11))
Output: 12:18:11 AM

TimeValue: The TimeValue function converts  an argument into the variant of subtype Date.

Syntax: TimeValue(time)

Msgbox(TimeValue("3:23:59 PM"))
Output: 3:23:59 PM

Examples: 
MyTime=Now
MyHour=Hour(MyTime)
Msgbox MyHour
Output: 17 at 5’o clock

MyVar=Minute(Now)
Msgbox MyVar
Output: 34 at 5:34 PM

 MySec=Second(Now)
Msgbox MySec
Output: 51 at 5:34:1 PM

For example your application date object allows the format dd-mm-yyyy format only. How to handle this?

Sysdate = Now
Sysyear = Year(Sysdate) 'Year is in yyyy format so need of any changes
Sysmonth=Month(Sysdate)  'Here month can be one or two digits
Mm=Len(Sysmonth)         'getting the length of the string

If mm < 2 then
  Retmonth="0"&mm  'If the month is one digit then we can concatenate 0 to month
Else
   Retmonth=mm
End if
Sysday=Day(Sysdate)  'Here day can be one or two digits
dd=Len(Sysday)       'getting the length of the string

If dd < 2 then
   Retday="0"&dd  'If the day is one digit then we can concatenate 0 to day
Else
   Retday=dd
End if

'Now concatenate the strings to required format dd-mm-yyyy
reqFormat = Retday&"-"&Retmonth&"-"&SysYear
msgbox reqFormat


No comments:

Post a Comment

Note: only a member of this blog may post a comment.