Math with Dates

jfloan

New member
Joined
May 19, 2008
Messages
3
Programming Experience
Beginner
Hello,

I am using VB.NET 2.0 to write an application. One function that I need to do is count the number of specific days within a user defined date range. For example, how many Mondays are there between Date1 and Date2.

Any ideas? Thanks in advance.
 
VB.NET:
Dim startDate As Date = #6/19/1969#
Dim endDate As Date = #5/21/2008#

Dim time As TimeSpan = endDate - startDate
Dim days As Integer = time.Days
Dim weeks As Integer = days \ 7
Dim remainder As Integer = days Mod 7

Dim mondays As Integer = weeks

For count As Integer = 0 To remainder - 1 Step 1
    If endDate.AddDays(count).DayOfWeek = DayOfWeek.Monday Then
        mondays += 1
        Exit For
    End If
Next count

MessageBox.Show(String.Format("There are {0} Mondays from {1:d} to {2:d}.", _
                              mondays, _
                              startDate, _
                              endDate))
 
Back
Top