Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))

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

Категории