Recipe 7.12. Adding to Dates and Times


You want to manipulate a Date value by adding an amount of time to it.


Sample code folder: Chapter 07\AddTime

Use one of the Date functions, such as AddYears() or AddMinutes(), to add specific units of time to a Date's current value.


There are seven "Add" functions used to add specific units of time to a Date:

  • AddYears()

  • AddMonths()

  • AddDays()

  • AddHours()

  • AddMinutes()

  • AddSeconds()

  • AddMilliseconds()

Each function adds a given amount of time to the Date. Confusion may arise because the parameters passed to some of these functions must be integers, while others require double-precision floating-point numbers. You can add only integer numbers of years, months, and hours, but you can add values with fractional parts to the days, minutes, seconds, and milliseconds. This is usually not a problem, but be aware that the various functions do require different types of parameters.

The following code demonstrates the "Add" functions by adding various amounts of time to the current date and time:

 Dim results As New System.Text.StringBuilder Dim rightNow As Date = Now ' ----- Show the current date and time. results.AppendLine("RightNow: " & rightNow.ToString) results.AppendLine() ' ----- Add date values. results.AppendLine("RightNow.AddYears(2): " & _    rightNow.AddYears(2)) results.AppendLine("RightNow.AddMonths(3): " & _    rightNow.AddMonths(3)) results.AppendLine("RightNow.AddDays(4): " & _    rightNow.AddDays(4)) ' ----- Add time values. results.AppendLine("RightNow.AddHours(5): " & _    rightNow.AddHours(5)) results.AppendLine("RightNow.AddMinutes(6): " & _    rightNow.AddMinutes(6)) results.AppendLine("RightNow.AddSeconds(7): " & _    rightNow.AddSeconds(7)) results.AppendLine("RightNow.AddMilliseconds(8000): " & _    rightNow.AddMilliseconds(8000)) ' ----- Display the results. MsgBox(results.ToString()) 

Figure 7-12 shows the date and time "right now," and the results of adding the various amounts of time to this value.

Adding a number of years or days accurately can be tricky because the addition can be defined in more than one way. For example, if one month is added to August 31, 2005, you might expect a result of October 1, 2005 because there are only 30 days in September. However, the result of adding one month to either August 30 or August 31 is September 30.

Figure 7-12. Using the "Add" category of Date functions to add various amounts of time to a Date

Similarly, adding one year to February 29, 2004 results in a date of February 28, 2005, instead of March 1, 2005. The variable lengths of months and years are ignored when adding these units of time.

The hard-to-define lengths of years and months could explain why these units are added as integer parameters in the functions described earlier. However, hours are well-defined, invariable units of time, yet AddHours() also requires an integer parameter. Go figure (literally)!

Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: