Link to home
Start Free TrialLog in
Avatar of AidenA
AidenA

asked on

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

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)
      Next

   End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
"Every second Monday" would be an addition of 14 days, would it not?
Dim selectedDate As DateTime = New DateTime(2011, 06, 15)

...

Dim nextDate As DateTime = selectedDate

For i As Integer = 0 To 99  ' 100 occurrences
    nextDate = nextDate.AddDays(14)

    ' Do something with nextDate
Next

Open in new window

Avatar of Member_6283346
Member_6283346

Hi! I don't see any big problem here - just get date of first monday like this

Dim startDate As DateTime
If DateTime.Today.DayOfWeek = DayOfWeek.Monday Then
   startDate  =  DateTime.Today
Else
  startDate = DateTime.Today.AddDays(8 - DateTime.Today.DayOfWeek)
End If

Then you add 7*n days to get date of nth Monday starting from current day

Dim nthMonday = startDate.AddDays(7*n)

Avatar of AidenA

ASKER

Hi thanks, ok so there's no easy way then (i.e a simple vb function which can do something similar)...

ok, i'd probably really need to calculate the number of weeks between the current date and the start date of the task... and if that can be divided by 14 then i should send the reminder...

actually that's a reasonably simple solution i suppose...

 
Dim dt As New DateTime(2011, 6, 29)
Dim dtcurr = Now.Date

If DateDiff(DateInterval.Day, dtcurr, dt) Mod 14 = 0 Then
  'this is a date which falls on a multiple of two weeks from the date dt
End If

Open in new window


OK I guess that solves my problem then, so codecruser mentioned 14 first which made me think of that