Recall the VB keywords that reference the current date and/or time:
- Now A vb6 date function that returns the current date and time together
- Date Returns the current date
- Time Returns the current time
For the examples that follow, assume that the current Date/Time (Now) is Friday, August 31, 2001at 9:15:20 PM.
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 vb6 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 vb6 date:
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
|
To demonstrate the date functions shown thus far, set up a "Try It" project, and place the following code in the cmdTryIt_Click event:
Private Sub cmdTryIt_Click()
Print "Now:"; Tab(30); Now
Print "Using DateValue:"; Tab(30); DateValue(Now)
Print "Using TimeValue:"; Tab(30); TimeValue(Now)
Print "Using Weekday:"; Tab(30); Weekday(Now)
Print "Using WeekdayName:"; Tab(30); WeekdayName(Weekday(Now))
Print "Using WeekdayName (abbrev.):"; Tab(30); WeekdayName(Weekday(Now), True)
Print "Using Month:"; Tab(30); Month(Now)
Print "Using MonthName:"; Tab(30); MonthName(Month(Now))
Print "Using MonthName (abbrev.):"; Tab(30); MonthName(Month(Now), True)
Print "Using Day:"; Tab(30); Day(Now)
Print "Using Year:"; Tab(30); Year(Now)
Print "Using Hour:"; Tab(30); Hour(Now)
Print "Using Minute:"; Tab(30); Minute(Now)
Print "Using Second:"; Tab(30); Second(Now)
End Sub
Run the project and click the "Try It" button. The output should look similar to the following:
Download the VB project code for the example above here.
The DatePart Function
The generic DatePart function returns an Integer containing the specified part of a given date/time value. Thus, it incorporates the functionality of the Weekday, Month, Day, Year, Hour, Minute, and Second functions. In addition, it can used to get the quarter of a given date (1 through 4) , the "Julian" date (the day of the year from 1 to 366), and the week number (1 through 53).
Syntax:
DatePart(interval, date[,firstdayofweek[, firstweekofyear]])
The DatePart function syntax has these parts:
Part
|
Description
| |||||||||||||||||||||||||||||||||
interval
|
Required. String expression that is the interval of time you want to return.
The string expression can be any of the following:
| |||||||||||||||||||||||||||||||||
date
|
Required. Date value that you want to evaluate.
| |||||||||||||||||||||||||||||||||
firstdayofweek
|
Optional. A constant that specifies the first day of the week. If not specified, Sunday is assumed.
| |||||||||||||||||||||||||||||||||
firstweekofyear
|
Optional. 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.
|
To demonstrate DatePart, set up a "Try It" project, and place the following code in the cmdTryIt_Click event:
Private Sub cmdTryIt_Click()
Print "Current date/time is: "; _
Format$(Now, "Long Date"); _
Spc(1); _
Format$(Now, "Long Time")
Print "*** DatePart Function Examples ***"
Print "Using 'yyyy':"; Tab(20); DatePart("yyyy", Now)
Print "Using 'q':"; Tab(20); DatePart("q", Now)
Print "Using 'm':"; Tab(20); DatePart("m", Now)
Print "Using 'y':"; Tab(20); DatePart("y", Now)
Print "Using 'd':"; Tab(20); DatePart("d", Now)
Print "Using 'w':"; Tab(20); DatePart("w", Now)
Print "Using 'ww':"; Tab(20); DatePart("ww", Now)
Print "Using 'h':"; Tab(20); DatePart("h", Now)
Print "Using 'n':"; Tab(20); DatePart("n", Now)
Print "Using 's':"; Tab(20); DatePart("s", Now)
End Sub
Run the project and click the "Try It" button. The output should look similar to the following:
Download the VB project code for the example above here.
Piecing Separate Numbers Together to Form a Date or Time Value
In the previous examples, we saw ways to isolate parts of a date/time value. What if you need to go the "other way"? If you have the separate parts of a date/time value in different variables and want to piece them together to formulate a date or time, there are two functions you can use to do this:DateSerial and TimeSerial.
The DateSerial takes three numeric arguments: year, month, and day respectively. It returns a date based on those values.
Example:
Dim intYear As Integer
Dim intMonth As Integer
Dim intDay As Integer
Dim dtmNewDate As Date
intYear = 2001
intMonth = 9
intDay = 2
dtmNewDate = DateSerial(intYear, intMonth, intDay)
' returns 9/2/2001
The TimeSerial takes three numeric arguments: hour, minute, and second respectively. It returns a time based on those values.
Example:
Dim intHour As Integer
Dim intMinute As Integer
Dim intSecond As Integer
Dim dtmNewTime As Date
intHour = 11
intMinute = 34
intSecond = 44
dtmNewTime = TimeSerial(intHour, intMinute, intSecond)
'returns 11:34:44 (AM)