There are many ways to assemble a Date value. For example, you can use a Date constant, such as the following:
StartDate = #10/17/2008 9:25:33 P.M.#
but more often you’ll build a Date value using one of the many functions that VBA gives you. The DateSerial function builds a Date value from its year/month/day components; similarly, the TimeSerial function builds a Time value from its hour/minute/second components:
Print DateSerial(2008, 10, 17) ‘ Displays “10/17/2008″
Print TimeSerial(12, 20, 30) ‘ Displays “12:20:30 P.M.”
‘ Note that they don’t raise errors with invalid arguments.
Print DateSerial(2008, 4, 31) ‘ Displays “5/1/2008″
The DateSerial function is also useful for determining indirectly whether a particular year is a leap year:
Function IsLeapYear(year As Integer) As Boolean
‘ Are February 29 and March 1 different dates?
IsLeapYear = DateSerial(year, 2, 29) <> DateSerial(year, 3, 1)
End Function
The DateValue and TimeValue functions return the date or time portions of their argument, which can be a string or a Date expression:
‘ The date a week from now
Print DateValue(Now + 7) ‘ Displays “10/17/2008″
A bunch of VBA functions let you extract date and time information from a Date expression or variable. The Day, Month, and Year functions return date values, whereas the Hour, Minute, and Second functions return time values:
‘ Get information about today’s date.
y = Year(Now): m = Month(Now): d = Day(Now)
‘ These functions also support any valid date format.
Print Year(“10/17/2008 9:25:33 P.M.”) ‘ Displays “2008″
The Weekday function returns a number in the range 1 through 7, which corresponds to the day of the week of a given Date argument:
Print Weekday(“10/17/2008″) ‘ Displays “6″ (= vbFriday)
The Weekday function returns 1 when the date is the first day of the week. This function is locale aware, which means that under different localizations of Microsoft Windows it could consider the first day of the week to be different from vbSunday. In most cases, this condition doesn’t affect the structure of your code. But if you want to be sure that 1 means Sunday, 2 means Monday, and so on, you can force the function to return a consistent value under all Windows systems, as follows:
Print Weekday(Now, vbSunday)
Although using the optional second argument forces the function to return the correct value, it doesn’t change the system localization. If you next call the Weekday function without the second argument, it will still consider the first day of the week to be what it was before.
Finally you can extract any date and time information from a Date value or expression using the DatePart function, for which the syntax is
Result = DatePart(Interval, Date, [FirstDayOfWeek], [FirstWeekOfYear])
You’ll rarely need to resort to this function because you can do most of your calculations using the other functions I’ve shown you so far. In two cases, however, this function is really useful:
‘ The quarter we are in
Print DatePart(“q”, Now) ‘ Displays “3″
‘ The week number we are in (# of weeks since Jan 1st)
Print DatePart(“ww”, Now) ‘ Displays “33″
The first argument can be one of the String constants listed in the following table. For more information about the two optional arguments, see the description of the DateAdd function in the next section.

Categories
Tag Cloud
Blog RSS
Comments RSS
Last 50 Posts
Back
Back
Back
Void « Default
Life
Earth
Wind
Water
Fire
Light 