Date and Time

1.15 Date and Time

DateAdd Function

Microsoft.VisualBasic.DateAndTime


DateAdd(interval, number, datevalue)

 

interval required; String or DateInterval enum

The interval of time to add

number required; Double

An expression denoting the number of time intervals you want to add (it can be positive or negative)

datevalue required; Date, or an expression capable of conversion to a date

The starting date to which the interval is to be added

Return Value

A past or future Date that reflects the result of the addition

Description

Returns a Date representing the result of adding (or subtracting, if number is negative) a given number of time periods to or from a given date. For instance, you can calculate the date 178 months before today's date, or the date and time 12,789 minutes from now.

interval can be one of the following literal strings:

yyyy

Year

q

Quarter

m

Month

y

Day of year

d

Day

w

Weekday

ww

Week

h

Hour

n

Minute

s

Second

interval can also be a member of the DateInterval enum:

Enum DateInterval
  Day
  DayOfYear
  Hour
  Minute
  Month
  Quarter
  Second
  Week
  Weekday
  WeekOfYear
End Enum

If number is positive, the result will be in the future; if number is negative, the result will be in the past. (The meaning of "future" and "past" here is relative to datevalue.)

The DateAdd function has a built-in calendar algorithm to prevent it from returning an invalid date. You can add 10 minutes to 31 December 1999 23:55, and DateAdd automatically recalculates all elements of the date to return a valid date, in this case 1 January 2000 00:05. This includes leap years; the calendar algorithm takes the presence of 29 February into account for leap years.

DateDiff Function

Microsoft.VisualBasic.DateAndTime


DateDiff(interval, date1, date2[, dayofweek[, weekofyear]])

 

interval required; String or DateInterval enum

Specifies the time unit used to express the difference between date1 and date2

date1, date2 required; Date or a literal date

The starting and ending dates, whose difference is computed as date2-date1

dayofweek optional; FirstDayOfWeek enum

A member of the FirstDayOfWeek enum

weekofyear optional; FirstWeekOfYear enum

A member of the FirstWeekOfYear enum

Return Value

A Long specifying the number of time intervals between the two dates

Description

Calculates the number of time intervals between two dates. For example, you can use the function to determine how many days there are between 1 January 1980 and 31 May 1998.

interval can be one of the following literal strings:

yyyy

Year

q

Quarter

m

Month

y

Day of year

d

Day

w

Weekday

ww

Week

h

Hour

n

Minute

s

Second

interval can also be a member of the DateInterval enum:

Enum DateInterval
  Day
  DayOfYear
  Hour
  Minute
  Month
  Quarter
  Second
  Week
  Weekday
  WeekOfYear
End Enum

When interval is Weekday or "w", DateDiff returns the number of weeks between the two dates. If date1 falls on a Monday, DateDiff counts the number of Mondays until date2. It counts date2, but not date1. If interval is Week or "ww", however, DateDiff returns the number of calendar weeks between the two dates. It counts the number of Sundays between date1 and date2. DateDiff counts date2 if it falls on a Sunday, but it doesn't count date1, even if it does fall on a Sunday.

DatePart Function

Microsoft.VisualBasic.DateAndTime


DatePart(interval, datevalue[,firstdayofweekvalue[, _
  firstweekofyearvalue]])

 

interval required; String or DateInterval enum

Defines the part of the date/time to extract from datevalue

datevalue required; Date, or an expression capable of conversion to a date

The Date value to evaluate

firstdayofweekvalue optional; FirstDayOfWeek enum

A member of the FirstDayOfWeek enum

firstweekofyearvalue optional; FirstWeekOfYear enum

A member of the FirstWeekOfYear enum

Return Value

An Integer containing the specified part

Description

Extracts an individual component of the date or time (like the month or the second) from a date/time value. The DatePart function returns an Integer containing the specified portion of the given date. DatePart is a single function encapsulating the individual Year, Month, Day, Hour, Minute, and Second functions.

interval can be one of the following literal strings:

yyyy

Year

q

Quarter

m

Month

y

Day of year

d

Day

w

Weekday

ww

Week

h

Hour

n

Minute

s

Second

interval can also be a member of the DateInterval enum:

Enum DateInterval
  Day
  DayOfYear
  Hour
  Minute
  Month
  Quarter
  Second
  Week
  Weekday
  WeekOfYear
End Enum

The firstdayofweekvalue argument can be any of the following members of the FirstDayOfWeek enumeration:

Enum FirstDayOfWeek
  System  'uses first day of week
  'setting on local system
  Sunday
  Monday
  Tuesday
  Wednesday
  Thursday
   Friday
  Saturday
End Enum

The firstdayofweekvalue argument affects only calculations that use either the Week (or "w") or Weekday (or "ww") interval values.

The firstweekofyearvalue argument can be any of the following members of the FirstWeekOfYear enumeration:

FirstWeekOfYear constant

Value

Description

System

0

Uses the local system setting

Jan1

1

Starts with the week in which January 1 occurs (the default value)

FirstFourDays

2

Starts with the first week that has at least four days in the new year

FirstFullWeek

3

Starts with the first full week of the year

 

DateSerial Function

Microsoft.VisualBasic.DateAndTime


DateSerial(year, month, day)

 

year required; Integer

Number between 100 and 9999, inclusive, or a numeric expression

month required; Integer

Any numeric expression between 1 and 12 to express the month

day required; Integer

Any numeric expression between 1 and 31 to express the day

Return Value

A Date representing the date specified by the arguments

Description

Returns a Date whose value is specified by the three date components (year, month, and day).

For the function to succeed, all three components must be present, and all must be numeric values. The value returned by the function takes the short date format defined by the Regional Settings applet in the Control Panel of the client machine.

If the value of a particular element exceeds its normal limits, DateSerial adjusts the date accordingly. For example, DateSerial(96,2,31) February 31, 1996 returns March 2, 1996.

You can specify expressions or formulas that evaluate to individual date components as parameters to DateSerial. For example, DateSerial(98,10+9,23) returns 23 March 1999. This makes it easier to use DateSerial to form dates whose individual elements are unknown at design time or that are created on the fly as a result of user input.

DateString Property

Microsoft.VisualBasic.DateAndTime


DateString(  )

 

Return Value

A String representing the current system date in the format "mm-dd- yyyy"

Description

Returns or sets a string representing the current system date. The allowed formats for setting the date are "m-d-yyyy," "m-d-y," "m/d/yyyy," and "m/d/y."

Day Function

Microsoft.VisualBasic.DateAndTime


Day(datevalue)

 

datevalue required; Date or literal date

Date whose day number is to be extracted

Return Value

An Integer from 1 to 31, representing the day of the month

Description

Returns an Integer ranging from 1 to 31, representing the day of the month of datevalue. The range of datevalue is 1/1/1 to 12/31/9999.

With OptionStrictOn, you must first convert datevalue to a Date data type before passing it to the Day function. You can use the CDate function for this purpose.

If the day portion of datevalue is outside of its valid range, the function generates an exception. This is also true if the day and month portion of datevalue is 2/29 for a non-leap year.

GetTimer Function

Microsoft.VisualBasic.VBMath


GetTimer(  )

 

Return Value

A Double indicating the number of seconds

Description

Returns the number of seconds since midnight.

Hour Function

Microsoft.VisualBasic.DateAndTime


Hour(timevalue)

 

timevalue required; Date

Date variable or literal date whose hour component is to be extracted

Return Value

An Integer from 0 to 23, specifying the hour of the day

Description

Extracts the hour element from a time expression. Regardless of the time format passed to Hour, the return value will be a whole number between 0 and 23, representing the hour of a 24-hour clock. If time contains Nothing, 0 is returned, so be careful here to check for Nothing.

Minute Function

Microsoft.VisualBasic.DateAndTime


Minute(TimeValue)

 

TimeValue required; Date

Date variable or literal date

Return Value

An Integer between 0 and 59, representing the minute of the hour

Description

Extracts the minute component from a given date/time expression

If TimeValue is not a valid date/time expression, the function generates an exception. To prevent this, use the IsDate function to check the argument before calling the Minute function. If TimeValue contains Nothing, 0 is returned, so be careful here to check for Nothing.

Month Function

Microsoft.VisualBasic.DateAndTime


Month(datevalue)

 

datevalue required; Date

Date variable or literal date whose month component is to be extracted.

Return Value

An Integer between 1 and 12

Description

Returns an integer representing the month of the year of a given date expression. If datevalue contains Nothing, Month returns Nothing.

MonthName Function

Microsoft.VisualBasic.DateAndTime


MonthName(month [, abbreviate])

 

month required; Integer

The ordinal number of the month, from 1 to 12

abbreviate optional; Boolean

A flag to indicate if an abbreviated month name should be returned

Return Value

String containing the name of the specified month

Description

Returns the month name of a given month. For example, a month of 1 returns January or (if abbreviate is True) Jan. The default value for abbreviate is False.

Now Property

Microsoft.VisualBasic.DateAndTime


Now(  )

 

Return Value

A Date containing the current system date and time

Description

Returns the current date and time based on the system setting. The date returned by Now takes the Windows General Date format based on the locale settings of the local computer. The U.S. setting for General Date is mm/dd/yy hh:mm:ss. The Now property is read-only.

Second Function

Microsoft.VisualBasic.DateAndTime


Second(timevalue)

 

timevalue required; Date

Date variable or literal date

Return Value

An Integer in the range 0 to 59, specifying the second in timevalue

Description

Extracts the seconds from a given time expression. If the time expression time is Nothing, the Second function returns 0.

TimeOfDay Property

Microsoft.VisualBasic.DateAndTime


TimeOfDay

 

Return Value

Date value giving the current system time

Description

Sets or returns the current system time.

Timer Property

Microsoft.VisualBasic.DateAndTime


Timer

 

Return Value

Double representing the number of seconds that have elapsed since midnight

Description

Returns the number of seconds since midnight.

TimeSerial Function

Microsoft.VisualBasic.DateAndTime


TimeSerial(hour, minute, second)

 

hour required; Integer

A number in the range 0 to 23

minute required; Integer

Any valid integer

second required; Integer

Any valid integer

Return Value

A Date representing the time specified by the arguments to the function

Description

Constructs a valid time given a number of hours, minutes, and seconds. Any of the arguments can be specified as relative values or expressions. The hour argument requires a 24-hour clock format; however, the returned time is determined by the system's regional settings.

If any value is greater than the normal range for the time unit to which it relates, the next higher time unit is increased accordingly. For example, a second argument of 125 will be evaluated as 2 minutes, 5 seconds.

If any value is less than zero, the next higher time unit is decreased accordingly. For example, TimeSerial(2,-1,30) returns 01:59:30.

TimeString Property

Microsoft.VisualBasic.DateAndTime


TimeString(  )

 

Return Value

String representing the current system time

Description

The TimeString property returns the current system time in the format determined by the system's regional settings. You can use any time format recognized by IsDate when setting the time using the TimeString property.

The string returned by the TimeString property also includes an invalid date, 01/01/0001. It can be eliminated with the Format or FormatDateTime function as follows:

Format(TimeOfDay(  ), "Long Time")
FormatDateTime(TimeOfDay(  ), DateFormat.LongTime)

TimeValue Function

 


 

 

See TimeValue Function entry under Section 1.13.

Weekday Function

Microsoft.VisualBasic.DateAndTime


Weekday(datevalue, [dayofweek])

 

date required; Date or valid date expression

Any valid date expression

dayofweek optional; Constant of FirstDayOfWeek enumeration

A constant indicating the first day of the week

Return Value

Integer

Description

Determines the day of the week of a given date. The default for dayofweek is FirstDayOfWeek.Sunday.

To determine the day of the week, think of the day specified by dayofweek as day 1, and the value returned by the function as indicating the day relative to day 1. Then, for example, if the return value of WeekDay is 2, this specifies the day following dayofweek. A return value of 1 specifies dayofweek. A return value of 7 specifies the day before dayofweek.

The members of the FirstDayOfWeek enumeration are:

Constant

Value

Description

Sunday

1

Sunday

Monday

2

Monday

Tuesday

3

Tuesday

Wednesday

3

Wednesday

Thursday

4

Thursday

Friday

5

Friday

Saturday

6

Saturday

Sunday

7

Sunday

Passing a value of 0 as the dayofweek argument uses the system's locale settings to determine the first day of the week.

WeekdayName Function

Microsoft.VisualBasic.DateAndTime


WeekdayName(Weekday, [abbreviate [, FirstDayOfWeekValue]])

 

Weekday required; Long

The ordinal number of the required weekday, from 1 to 7

abbreviate optional; Boolean

Specifies whether to return the full day name or an abbreviation

FirstDayOfWeekValue optional; FirstDayOfWeek constant

Member of the FirstDayOfWeek enum indicating the first day of the week

Return Value

A String

Description

Returns the name of the day. Weekday must be a number between 1 and 7, or the function generates an ArgumentException error.

The default value of abbreviate is False. For a list of the members of the FirstDayOfWeek enumeration, see the "Weekday Function" entry. The default value of FirstDayOfWeekValue is FirstDayOfWeek.Monday.

Year Function

Microsoft.VisualBasic.DateAndTime


Year(datevalue)

 

datevalue required; Date or valid date expression

Any valid date expression

Return Value

Integer

Description

Returns an integer representing the year in a given date expression. If OptionStrict is off and datevalue contains Nothing, Year returns 1. For example:

Dim oDat As Object
Console.Writeline(Year(sDat))  ' Displays 1

If datevalue is a date literal (a date delimited with the # symbol), the year must contain four digits.

 



VB. NET Language Pocket Reference
VB.NET Language Pocket Reference
ISBN: 0596004281
EAN: 2147483647
Year: 2002
Pages: 31

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net