Recipe 7.16. Determining the Day of the Year for a Date


Problem

You want to calculate the day of the year for a date, a number in the range 1 to 366.

Solution

Use the Date object's DayOfYear property to get this number directly, with no calculations required.

Discussion

The following code shows how to determine a date's day of the year:

 Dim rightNow As Date = Now Dim yearDay As Integer = rightNow.DayOfYear Dim results As String = String.Format( _    "Day of year for {0:D}: {1}", Now, yearDay) MsgBox(results) 

Figure 7-16 shows the day of the year for a date, as displayed by the message box in the sample code.

Figure 7-16. Determining the day of year for a specific date with the Date object's DayOfYear property





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

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net