Sunday, October 3, 2010

Visual Basic Date and Time Function : DatePart

Purpose : Returns a Variant (Integer) containing the specified part of a given date.

Syntax :
DatePart (interval, date [ , firstdayofweek [ , firstweekofyear ] ] )

Interval : that is the interval of time you want to return.
date : value that you want to evaluate.
Firstdayofweek : A constant that specifies the first day of the week. If not specified, Sunday is assumed.
Firstweekofyear : A constant that specifies the first week of the year. If not specified, the first week is assumed to be the week in which January 1 occurs.

You can use the DatePart function to evaluate a date and return a specific interval of time. For example, you might use DatePart to calculate the day of the week or the current hour.

Dim TheDate As Date ' Declare variables.
Dim Msg
TheDate = InputBox ("Enter a date:")
Msg = "Quarter: " & DatePart("q", TheDate)
MsgBox Msg

No comments:

Post a Comment