Advice

I am a little caught about by my own functionality. I have a schedule object where one of the options is to set a reminder for every week, in addition I also allow them to set it to run on specifc days of the week.

So for example they could have a job run every week on Mon, Tues and Fri.

The problem I have is this

If the next run date is set to the 12 (this week), I am assuming I should set the next due date to the 13th, then the 16th and then the 19th and so on.

Is the easier option to check what day it is then add on so many days?
Is there an easy way of doing this?

Any help would be appreciated

rocky050371Asked:
Who is Participating?
 
CodeCruiserCommented:
You are checking the day of the week of schedule and then based on today's day of week, you are adding the days to the schedule? So if schedule is to run on thursday and today is monday, you are adding 3?
0
 
Éric MoreauSenior .Net ConsultantCommented:
you have to create a property getter/setter:

    Private _myprop As String
    Public Property MyProp() As String
        Get
            Return _myprop
        End Get
        Set(ByVal value As String)
            _myprop = value
        End Set
    End Property
0
 
CodeCruiserCommented:
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
CodeCruiserCommented:
You can check the DayOfWeek against each date and keep adding 1 day to it so that it becomes that day.

For example

Dim dt as DateTime = Now

If dt.DayOfWeek <> DayOfWeek.Monday Then
   While dt.DayOfWeek <> DayofWeek.Monday
          dt = dt.AddDays(1)
   End while
End If
0
 
rocky050371Author Commented:
I have this so far, can anyone think of a better way


  If schedule.SchedFrequencyIntervalException <> 0 Then
                                            Select Case schedule.SchedNextRun.DayOfWeek
                                                Case DayOfWeek.Monday
                                                    If CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Tuesday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(1)
                                                    End If
                                                    If CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Wednesday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(2)
                                                    End If
                                                    If CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Thursday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(1)
                                                    End If
                                                    If CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Friday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(2)
                                                    End If
                                                    If CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Saturday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(1)
                                                    End If
                                                    If CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Sunday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(2)
                                                    End If
                                                Case DayOfWeek.Tuesday
                                                    If CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Wednesday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(1)
                                                    End If


                                            End Select






                                        Else
                                            schedule.SchedNextRun = schedule.SchedNextRun.AddDays(schedule.SchedFrequencyInterval * 7)
                                        End If
0
 
rocky050371Author Commented:
The code has not been validated and needs a couple more branches, it just seems a bit code intensive
0
 
rocky050371Author Commented:
Right this is closer to what it would look like

 If schedule.SchedFrequencyIntervalException <> 0 Then
                                            Select Case schedule.SchedNextRun.DayOfWeek
                                                Case DayOfWeek.Monday
                                                    If CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Tuesday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(1)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Wednesday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(2)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Thursday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(3)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Friday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(4)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Saturday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(5)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Sunday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(6)
                                                    Else
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(schedule.SchedFrequencyInterval * 7)
                                                    End If
                                                Case DayOfWeek.Tuesday
                                                    If CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Wednesday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(1)
                                                    End If

                                            End Select

                                        Else
                                            schedule.SchedNextRun = schedule.SchedNextRun.AddDays(schedule.SchedFrequencyInterval * 7)
                                        End If
0
 
rocky050371Author Commented:
Right ignore the rest :-), any advice on this

Case FrequencyType.Weeks

                                        iDayWeekConstant = schedule.SchedFrequencyInterval * 7

                                        If schedule.SchedFrequencyIntervalException <> 0 Then

                                            Select Case schedule.SchedNextRun.DayOfWeek
                                                Case DayOfWeek.Monday
                                                    If CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Tuesday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(1)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Wednesday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(2)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Thursday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(3)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Friday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(4)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Saturday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(5)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Sunday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(6)
                                                    Else
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(iDayWeekConstant)
                                                    End If
                                                Case DayOfWeek.Tuesday
                                                    If CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Wednesday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(1)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Thursday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(2)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Friday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(3)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Saturday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(4)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Sunday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(5)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Monday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(6 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    Else
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(iDayWeekConstant)
                                                    End If
                                                Case DayOfWeek.Wednesday
                                                    If CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Thursday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(1)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Friday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(2)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Saturday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(3)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Sunday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(4)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Monday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(5 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Tuesday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(6 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    Else
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(iDayWeekConstant)
                                                    End If
                                                Case DayOfWeek.Thursday
                                                    If CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Friday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(1)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Saturday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(2)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Sunday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(3)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Monday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(4 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Tuesday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(5 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Wednesday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(6 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    Else
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(iDayWeekConstant)
                                                    End If
                                                Case DayOfWeek.Friday
                                                    If CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Saturday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(1)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Sunday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(2)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Monday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(3 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Tuesday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(4 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Wednesday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(5 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Thursday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(6 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    Else
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(iDayWeekConstant)
                                                    End If
                                                Case DayOfWeek.Saturday
                                                    If CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Sunday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(1)
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Monday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(2 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Tuesday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(3 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Wednesday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(4 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Thursday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(5 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Friday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(6 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    Else
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(iDayWeekConstant)
                                                    End If
                                                Case DayOfWeek.Sunday
                                                    If CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Monday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(1 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Tuesday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(2 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Wednesday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(3 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Thursday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(4 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Friday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(5 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    ElseIf CheckDayFlag(CType(schedule.SchedFrequencyIntervalException, DaysOfTheWeek), DaysOfTheWeek.Saturday) Then
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(6 + If(iDayWeekConstant > 7, iDayWeekConstant - 7, 0))
                                                    Else
                                                        schedule.SchedNextRun = schedule.SchedNextRun.AddDays(iDayWeekConstant)
                                                    End If
                                            End Select
                                        Else
                                            schedule.SchedNextRun = schedule.SchedNextRun.AddDays(iDayWeekConstant)
                                        End If

Open in new window

0
 
rocky050371Author Commented:
Yes, it was just to see if there is a better way
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.