Page 128 - Excel Progamming Weekend Crash Course
P. 128
h540629 ch08.qxd 9/2/03 9:33 AM Page 103
Session 8 — Working with Dates and Times 103
d2 = “12/31/” & year
If DateDiff(“d”, d1, d2) = 365 Then
IsLeapYear = True
Else
IsLeapYear = False
End If
End Function
Table 8-4 describes the other date information functions provided by VBA. In each case,
date is a type Date or a numeric or string expression that can be interpreted as a date.
Table 8-4 More Date and Time Functions in VBA
Function Description
Day(date) Returns a value 1–31 representing the day of the month
Hour(date) Returns a value 0–23 representing the hour of the day
Minute(date) Returns a value 0–59 representing the minute of the hour
Month(date) Returns a value 1–12 representing the month of the year
Second(date) Returns a value 0–59 representing the second of the minute
Weekday(date, Returns a value 1–7 representing the day of the week. The
firstdayofweek) optional firstdayofweek argument specifies the first day of
the week; the default is Sunday. See Table 8-2 for other values
for this argument.
Year(date) Returns the year
Formatting Date/Time Values
VBA is rather smart when it comes to displaying date and time values. Without any special
effort on your part, a date/time value will be displayed using the “short date format” as spec-
ified in your system settings. With Windows 2000, in the United States, this is as follows:
12/01/2003 9:35:00 AM
Date display settings are found in the Windows Control Panel under Regional
Settings or Regional Options (depending on your Windows version).
Tip
If you need more control over how dates and times are displayed, use the
FormatDateTime function. The syntax for this function is:
FormatDateTime(Date, NamedFormat)