News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

Pulling the Pieces Apart

Started by sukishan, Sep 04, 2009, 04:39 PM

Previous topic - Next topic

sukishan

Pulling the Pieces Apart
Of course, if you're working with dates, you're also working with years, months, days, hours, minutes, and seconds. You might also like to work with a date in terms of its placement within the year, or which quarter it's in, or which day of the week it is. VBA provides simple and useful functions for retrieving all this information, and more.

Retrieving Just the Part You Need

To start with, you'll find the functions listed in Table 2.2 to be helpful in extracting simple information from a date value. Each of these functions accepts a date parameter and returns an integer containing the requested piece of information. (You can also use the DatePart function, described in the section "One Function Does It All" later in this chapter, to retrieve any of these values. It's simpler to call the functions in Table 2.2 if you just need one of the values listed.)

Table 2.2: Simple Date/Time Functions

Function            Return Value
Year                 Year portion of the date
Month              Month portion of the date
Day                 Day portion of the date
Hour                Hour portion of the date
Minute             Minute portion of the date
Second           Seconds portion of the date


You can use any of these functions to retrieve a portion of a date value. For example, the following fragment displays the current year value:

MsgBox "The current year is " & Year(Now)and the following fragment displays the month and day:

MsgBox "Month: " & Month(Now) & " Day: " & Day(Now)The following fragment checks the current time and allows you to take an action at 1:12 p.m.:

If Hour(Time) = 13 And Minute(Time) = 12 Then
    ' You know it's 1:12 PM
End IfDon't try sending the Date function to functions that return time portions of a date/time value. Because the return value from the Date function doesn't include any time information (its fractional portion is 0), the Hour, Minute, and Second functions will all return 0. The same warning applies to the Day, Month, and Year functions: don't send them the Time function, because the return value from that function doesn't include any date information.
A good beginning makes a good ending