![]() |
Leading the way in Microsoft Office Development |
| Home | | | Excel | | | Word | | | PowerPoint | | | VBA | | | SharePoint | | | Consultancy | | | Newsletter | | | Contact |
| VBA > Dates & Times > Adding and Subtracting | < Previous | Next > |
VBA provides a data type for storing date and time values, the Date data type. Convenient as the Date data type is, manipulating date values in VBA can still be tricky. To easily work with dates, you need to understand how VBA stores date values internally. Note The Microsoft Office 2000 applications all support dates beyond the year 2000. However, there are some issues you need to take into account in order to design your application so that it properly handles both twentieth and twenty-first century dates |
Date Data Type |
VBA provides the Date data type to store date and time values. The Date data type is an 8-byte floating-point value, so internally it's the same as the Double data type. The Date data type can store dates between January 1, 100, and January 1, 9999. | ||
VBA stores the date value in the integer portion of the Date data type, and the time value in the decimal portion. The integer portion represents the number of days since December 30, 1899, which is the starting point for the Date data type. Any dates before this one are stored as negative numbers; all dates after are stored as positive values. If you convert a date value representing December 30, 1899, to a double, you'll find that this date is represented by zero. | ||
The decimal portion of a date represents the amount of time that has passed since midnight. For example, if the decimal portion of a date value is .75, three-quarters of the day has passed, and the time is now 6 P.M. | ||
Because the integer portion of a date represents number of days, you can add and subtract days from one date to get another date. |
Getting the Current Date and Time |
Three functions in VBA can tell you exactly when it is: the Now, Date, and Time functions. The Now function returns both the date and time portions of a Date variable. For example, calling the Now function from the Immediate window returns a value like this one: | ||
2/23/98 6:16:47 PM |
Formatting a Date |
You can use predefined formats to format a date by calling the FormatDateTime function, or you can create a custom format for a date by using the Format function. | ||
The following procedure formats a date by using both built-in and custom formats: |
|
Date Delimiters |
When you work with date literals in your code, you need to indicate to VBA that a value is a date. If you don't, VBA may think you're performing subtraction or floating-point division. | ||
For example, if you run the following fragment, the value that VBA assigns to the Date variable is not April 5, 1998, but 4 divided by 5 divided by 98. Because you're assigning it to a Date variable, VBA converts the number to a date, and prints "12:11:45 AM" to the Immediate window: |
|
To avoid this problem, you must include delimiters around the date. The preferred date delimiter for VBA is the number sign (#). You can also use double quotation marks, as you would for a string, but doing so requires VBA to perform an extra step to convert the string to a date. If you rewrite the fragment as follows to include the date delimiter, VBA prints "4/5/98" to the Immediate window: |
|
Assembling a Date |
To work with a date in code, you sometimes need to break it down into its component parts—that is, its day, month, and year. Once you've done this, you can perform a calculation on one element, and then reassemble the date. To break a date into components, you can use the Day, Month, and Year functions. Each of these functions takes a date and returns the day, month, or year portion, respectively, as an Integer value. For example, Year(#2/23/98#) returns "1998." | ||
To reassemble a date, you can use the DateSerial function. This function takes three integer arguments: a year, a month, and a day value. It returns a Date value that contains the reassembled date. | ||
Often you can break apart a date, perform a calculation on it, and reassemble it all in one step. For example, to find the first day of the month, given any date, you can write a function similar to the following one: |
|
The FirstOfMonth procedure takes a date or, if the calling procedure doesn't pass one, uses the current date. It breaks the date into its component year and month, and then reassembles the date using 1 for the day argument. Calling this procedure with the dteDate argument #2/23/98# returns "2/1/98". | ||
The following procedure uses the same strategy to return the last day of a month, given a date: |
|
Both of these procedures are available in the modDateTime module in VBA.mdb in the ODETools\V9\Samples\OPG\Samples\CH07 subfolder on the Office 2000 Developer CD-ROM. | ||
VBA also provides functions that you can use to disassemble and reassemble a time value in the same manner. The Hour, Minute, and Second functions return portions of a time value; the TimeSerial function takes an hour, minute, and second value and returns a complete time value. |
Getting Part of a Date |
The previous section showed how to return the year, month, and day from a date. You can get other information about a date as well, such as what quarter or week it falls in, or what day of the week it is. | ||
The Weekday function takes a date and returns a constant indicating on what day of the week it falls. The following procedure takes a date and returns True if the date falls on a workday—that is, Monday through Friday—and False if it falls on a weekend. |
|
This procedure is available in the modDateTime module in VBA.mdb in the ODETools\V9\Samples\OPG\Samples\CH07 subfolder on the Office 2000 Developer CD-ROM. | ||
In addition to the individual functions that return part of a date—Year, Month, Day, and Weekday—VBA includes the DatePart function, which can return any part of a date. Although it may seem redundant, the DatePart function gives you slightly more control over the values you return, because it gives you the option to specify the first day of the week and the first day of the year. For this reason, it can be useful when you're writing code that may run on systems in other countries. In addition, the DatePart function is the only way to return information about what quarter a date falls into. |
Adding and Subtracting Dates |
To add an interval to a given date, you must use the DateAdd function, unless you're adding days to a date. As mentioned earlier, since the integer portion of a Date variable represents the number of days that have passed since December 30, 1899, adding integers to a Date variable is equivalent to adding days. | ||
By using the DateAdd function, you can add any interval to a given date: years, months, days, weeks, quarters. The following procedure finds the anniversary of a given date; that is, the next date on which it occurs. If the anniversary has already occurred this year, the procedure returns the date of the anniversary in the next year. |
|
This procedure is available in the modDateTime module in VBA.mdb in the ODETools\V9\Samples\OPG\Samples\CH07 subfolder on the Office 2000 Developer CD-ROM. | ||
To find the interval between two dates, you can use the DateDiff function. The interval returned can be any of several units of time: days, weeks, months, years, hours, and so on. | ||
The following example uses the DateDiff function to return the day number for a particular day of the year. The procedure determines the last day of the last year by using the DateSerial function, and then subtracts that date from the date that was passed in to the procedure. |
|
Calling this procedure with the value of #2/23/98# returns "54." |
Calculating Elapsed Time |
You can use the DateAdd and DateDiff functions to calculate the time that has elapsed between two dates, and then, with a little additional work, present that time in the desired format. For example, the following procedure calculates a person's age in years, taking into account whether his or her birthday has already occurred in the current year. | ||
Using the DateDiff function to determine the number of years between today and a birthdate doesn't always give a valid result because the DateDiff function rounds to the next year. If a person's birthday hasn't yet occurred, using the DateDiff function will make the person one year older than he or she actually is. | ||
To remedy this situation, the procedure checks to see whether the birthday has already occurred this year, and if it hasn't, it subtracts 1 to return the correct age. |
|
| Copyright © 2011 Better Solutions Limited. All Rights Reserved. | < Previous | Top | Next > |