Next Monday

Hello,

How can I define the date of the next coming Monday (calculated from today onwards)


Answer this question

Next Monday

  • ElliotHC

    Hi,

    In your output you may want to use.>>

    Dim myDateString As String

    myDateString = myDate.ToLongDateString 'or

    myDateString = myDate.ToShortDateString

    if you prefer one of those formats.

     I believe it won't work on the end of the lines in this code as myDate

    is the full System.DateTime.

     


        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Dim myDate As System.DateTime
            myDate = Now
            Select Case myDate.DayOfWeek

                Case DayOfWeek.Monday
                    MsgBox("It is a Monday now, next Monday is " & myDate.Date.AddDays(7).ToString)
                Case DayOfWeek.Tuesday
                    MsgBox("Next Monday is " & myDate.Date.AddDays(6).ToString)
                Case DayOfWeek.Wednesday
                    MsgBox("Next Monday is " & myDate.Date.AddDays(5).ToString)
                Case DayOfWeek.Thursday
                    MsgBox("Next Monday is " & myDate.Date.AddDays(4).ToString)
                Case DayOfWeek.Friday
                    MsgBox("Next Monday is " & myDate.Date.AddDays(3).ToString)
                Case DayOfWeek.Saturday
                    MsgBox("Next Monday is " & myDate.Date.AddDays(2).ToString)
                Case DayOfWeek.Sunday
                    MsgBox("Next Monday is " & myDate.Date.AddDays(1).ToString)

            End Select

        End Sub

     

    Regards,

    S_DS

     



  • Harkernator

    Hi Frank

    Can I be as bold as to suggest a slightly 'better' alternative ...

    dim daysToAdd() as decimal = new decimal(){1, 7, 6, 5, 4, 3, 2}

    dim nextMon as datetime = mydate.adddays(daysToAdd(myDate.DayOfWeek))

    It's effectively the same code as in your example, but does away with the 8 boxing, 1 unboxing and type coersion operations, and takes some strain off of the garbage collector. I suspect there is also a saving in that there is no need to manage another stack (pretty sure Choose will not inline compile .. but would have to check to be sure either way).

    It's probably small change in the scope of things but may add up to a big performance gain if used in a tight loop.

    Richard


  • GaneshSharma

    Here's a one liner using the not often seen Choose function:

    MyDate.AddDays(Choose(MyDate.DayOfWeek + 1, 1, 7, 6, 5, 4, 3, 2))



  • Next Monday