WORKING WITH QTP

TestEveryThinG

Date Time Functions In Visual Basic

Posted by rajivkumarnandvani on September 28, 2010

The following functions isolate the date portion and time portion, respectively, of a Date/Time value:

Function

Description

DateValue Returns the date portion of a Date/Time value, with the time portion “zeroed out”. (Note: When the time portion of a date/time variable is “zeroed out”, the time would be interpreted as 12:00 AM.)

Example:

Dim dtmTest As Date

dtmTest = DateValue(Now)

At this point, the date portion of dtmTest is 8/31/2001, with a time portion of 0 (12:00 AM midnight).

TimeValue Returns the time portion of a Date/Time value, with the date portion “zeroed out”. (Note: When a date/time variable is “zeroed out”, the date will actually be interpreted as December 30, 1899.)

Example:

Dim dtmTest As Date

dtmTest = TimeValue(Now)

At this point, the time portion of dtmTest is 9:15:20 PM, with a date portion of 0 (12/30/1899).

The following functions are used to isolate a particular part of a date:

Function

Description

Weekday Returns a number from 1 to 7 indicating the day of the week for a given date, where 1 is Sunday and 7 is Saturday.

Example:

intDOW = Weekday(Now) ‘ intDOW = 6

Note:

When necessary to refer to a day of the week in code, VB has a set of built-in constants that can be used instead of the hard-coded values 1 thru 7:

Constant Value

vbSunday 1

vbMonday 2

vbTuesday 3

vbWednesday 4

vbThursday 5

vbFriday 6

vbSaturday 7

Function

Description

WeekdayName Returns a string containing the weekday name (“Sunday” thru “Saturday”), given a numeric argument with the value 1 through 7.

Example:

strDOW = WeekdayName(6) ‘ strDOW = “Friday”

The WeekdayName function takes an optional, second argument (Boolean) indicating whether or not to abbreviate the weekday name. By default, the second argument is False, meaning do not abbreviate and return the full name. If True, the first three letters of the weekday name will be returned:

Example:

strDOW = WeekdayName(6, True) ‘ strDOW = “Fri”

You can nest the Weekday function within the WeekdayName function to get the weekday name for a given date:

Example:

strDOW = WeekdayName(Weekday(Now)) ‘ strDOW = “Friday”

Month Returns a number from 1 to 12 indicating the month portion of a given date.

Example:

intMonth = Month(Now) ‘ intMonth = 8

MonthName Returns a string containing the month name (“January” thru “December”), given a numeric argument with the value 1 through 12.

Example:

strMoName = MonthName(8) ‘ strMoName = “August”

The MonthName function takes an optional, second argument (Boolean) indicating whether or not to abbreviate the month name. By default, the second argument is False, meaning do not abbreviate and return the full name. If True, the first three letters of the month name will be returned:

Example:

strMoName = MonthName(8, True) ‘ strMoName = “Aug”

You can nest the Month function within the MonthName function to get the month name for a given date:

Example:

strMoName = MonthName(Month(Now)) ‘ strMoName = “August”

Day Returns a number from 1 to 31 indicating the day portion of a given date.

Example:

intDay = Day(Now) ‘ intDay = 31

Year Returns a number from 100 to 9999 indicating the year portion of a given date.

Example:

intYear = Year(Now) ‘ intYear = 2001

The following functions are used to isolate a particular part of a time:

Function

Description

Hour Returns an integer specifying a whole number between 0 and 23 representing the hour of the day.

Example:

intHour = Hour(Now) ‘ intHour = 21 (for 9 PM)

Minute Returns an integer specifying a whole number between 0 and 59 representing the minute of the hour.

Example:

intMinute = Minute(Now) ‘ intMinute = 15

Second Returns an integer specifying a whole number between 0 and 59 representing the second of the minute.

Example:

intSecond = Second(Now) ‘ intSecond = 20

Advertisements

2 Responses to “Date Time Functions In Visual Basic”

  1. arefa said

    The post is really valuable and can be comprehended effectively in script

  2. Thanks 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: