Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1918
  • Last Modified:

How to find the date for Monday in vb.net

In VB6 I could use the following code to find the date for Monday of the current week:
dteMonday = Format(Date - (IIf(Weekday(Date) >= vbMonday, Weekday(Date) - vbMonday, 7 - (vbMonday - Weekday(Date)))))

Is there a way to do this in vb.net? Thanks!
1 Solution
I think that must give you previous Monday

        Dim dtMon As Date
        If Today.DayOfWeek = DayOfWeek.Monday Then
            dtMon = Today
            dtMon = Today.AddDays(1 - Today.DayOfWeek)
        End If
myesterAuthor Commented:
iboutchkine, thanks for the help. This is much easier to do in vb.net than in VB6. I had to make one small addition to account for if Today is Sunday. If Today is Sunday then dtMon was returing the next day vs the previous Monday. I.E. if Today is Sunday Feb 23rd dtMon was returing 02/24/2003 vs 02/17/2003 which is what I am looking for. I corrected for it like this:

Select Case Today.DayOfWeek
            Case DayOfWeek.Monday
                dtMon = Today
            Case DayOfWeek.Sunday
                dtMon = Today.AddDays(Today.DayOfWeek - 6)
            Case Else
                dtMon = Today.AddDays(1 - Today.DayOfWeek)
End Select

Thanks again for you help.

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Tackle projects and never again get stuck behind a technical roadblock.
Join Now