We help IT Professionals succeed at work.
Get Started

VB.NET working with dates (e.g. how to get every second monday from a particular date...)

Last Modified: 2012-05-11
Hi, I'm trying to set up a website where a user can enter in certain info and it will create a task (much like MS outlook) where it will send a reminder email but it seems to be getting quite complicated to set something like that up.

The situations i'm scratching my head over a bit are situations where i would want to calculate every second monday from when the task starts. I was hoping I could just note that the task started on the first monday of the month (for instance) and then just send off the reminder on the corresponding first and third mondays of every month (i.e. every second week). But I'm thinking that is not going to work is it? Because a month could have 5 mondays. So I guess the only way to do it is to (each time) calculate how many mondays there were from when the task started and note whether the current monday is an even or odd number and send a reminder accordingly. Alternatively I could just have a flag which notes whether a mail was send last week or not and toggle it, but that's not the most elegant solution and requires me to run two queries on the database each time.

Also, what's the easiest way of calculating the second monday of the current month? I've made a sub below which will do it... but i'm wondering if there is a better way to do this kind of manipulation
Sub CalcFirstDayOfWeek()

      Dim intMondays(5), intFirstDay, intWeek, intCount As Integer

      Dim dt As New DateTime(Now.Year, Now.Month, 1)
      intFirstDay = dt.DayOfWeek
      intWeek = 0

      For i As Integer = 1 To Date.DaysInMonth(Now.Year, Now.Month)
         If dt.DayOfWeek = DayOfWeek.Monday Then
            intMondays(intCount) = (dt.DayOfWeek - intFirstDay + 1) + (intWeek * 7)
            intCount += 1
         ElseIf dt.DayOfWeek = DayOfWeek.Sunday Then
            intWeek += 1
         End If
         dt = dt.AddDays(1)

   End Sub

Open in new window

Watch Question
Most Valuable Expert 2012
Top Expert 2014
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE