Tuesday, October 20, 2009

Piecing Separate Numbers Together to Form a Date or Time Value


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)

0 comments:

Your Ad Here