4

I'm trying to get the date of the monday in the current week. Is this close to correct?

Dim MondayOfCurrentWeek As Date = Date.Today - Date.Today.DayOfWeek + 1

As far as I understand it the AyOfWeek indexes are:

1 = Monday
2 = Tuesday
3 = Wednesday
4 = Thursday
5 = Friday
6 = Saturday
0 = Sunday

Hence if for example Date Today is a thursday I would get:

Dim MondayOfCurrentWeek As Date = Date - 4 + 1 

which would be equal to

Date - 3

and seems right to me.

Or am I totally off?

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
user1283776
  • 19,640
  • 49
  • 136
  • 276
  • It's C#, but there's a [semi-duplicate here](http://stackoverflow.com/questions/38039/how-can-i-get-the-datetime-for-the-start-of-the-week), where it has a nice function you can use for any day of the week. – James Thorpe Jan 12 '15 at 10:35
  • On the other side of things, whilst Enums _shouldn't_ change in future releases, there's always the chance that they _could_. I would be very wary of performing arithmetic based on the value of an Enum. – James Thorpe Jan 12 '15 at 10:40
  • Why on earth would he use an enum? Days of the week are builtin in the .NET framework. –  Jan 12 '15 at 10:46
  • 1
    @Hypenate [`DayOfWeek` _is_ an enum](http://msdn.microsoft.com/en-us/library/system.dayofweek(v=vs.90).aspx). – James Thorpe Jan 12 '15 at 10:47
  • Thought he wanted to create his own enum for that... –  Jan 12 '15 at 10:57
  • 1
    FWIW - isn't Sunday 0? – dbasnett Jan 12 '15 at 12:28
  • Sunday is 0, my mistake – user1283776 Jan 12 '15 at 15:15

5 Answers5

8

A very simple alternative approach, which while probably not as performant, but does avoid any arithmetic based errors:

Dim monday As Date = Date.Today
While (monday.DayOfWeek <> DayOfWeek.Monday)
    monday = monday.AddDays(-1)
End While

This could easily be extended out to a function to handle any day of the week. Unless this is a really high volume piece of code, performance will be fine.

James Thorpe
  • 31,411
  • 5
  • 72
  • 93
3

This should do what you want. It finds the previous Monday from the date supplied. There are no loops so will be quick and doesn't rely on the enum value to do arithmetic adjustment, so won't break if the enum values ever change:

Public Shared Function PreviousMonday(ByVal dateValue As DateTime) As DateTime
    Dim dayOffset As Integer
    Select Case dateValue.DayOfWeek
        Case DayOfWeek.Sunday : dayOffset = 6
        Case DayOfWeek.Monday : dayOffset = 0
        Case DayOfWeek.Tuesday : dayOffset = 1
        Case DayOfWeek.Wednesday : dayOffset = 2
        Case DayOfWeek.Thursday : dayOffset = 3
        Case DayOfWeek.Friday : dayOffset = 4
        Case DayOfWeek.Saturday : dayOffset = 5
    End Select

    Return dateValue.AddDays(-1 * dayOffset)
End Function
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
0

If you are looking for Monday to Sunday range of previous week, then you can get them by using these functions (modified version from the answer of Matt Wilko).

Public Shared Function PreviousMonday(ByVal dateValue As Date) As Date
Dim dayOffset As Integer
Select Case dateValue.DayOfWeek
    Case DayOfWeek.Sunday : dayOffset = -13
    Case DayOfWeek.Monday : dayOffset = -7
    Case DayOfWeek.Tuesday : dayOffset = -8
    Case DayOfWeek.Wednesday : dayOffset = -9
    Case DayOfWeek.Thursday : dayOffset = -10
    Case DayOfWeek.Friday : dayOffset = -11
    Case DayOfWeek.Saturday : dayOffset = -12
End Select

Return dateValue.AddDays(dayOffset)
End Function

Public Shared Function PreviousSunday(ByVal dateValue As Date) As Date
Dim dayOffset As Integer
Select Case dateValue.DayOfWeek
    Case DayOfWeek.Sunday : dayOffset = -7
    Case DayOfWeek.Monday : dayOffset = -1
    Case DayOfWeek.Tuesday : dayOffset = -2
    Case DayOfWeek.Wednesday : dayOffset = -3
    Case DayOfWeek.Thursday : dayOffset = -4
    Case DayOfWeek.Friday : dayOffset = -5
    Case DayOfWeek.Saturday : dayOffset = -6
End Select

Return dateValue.AddDays(dayOffset)
End Function
Timo Riikonen
  • 477
  • 5
  • 8
0
var now = DateTime.Now; //17.09.2020 19:15:49
int dayInWeek = (int)now.DayOfWeek; //4
var previousMonday = now.AddDays(-(((dayInWeek + 6) % 7) + 7)); //-10
var previousSunday= previousMonday.AddDays(6); //-4
        
Console.WriteLine(previousMonday); //07.09.2020 19:15:49
Console.WriteLine(previousSunday); //13.09.2020 19:15:49
Evan
  • 23
  • 4
  • Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. – DCCoder Sep 17 '20 at 22:54
-1
Enumerable.Range(0,7).Select(Function(r) Date.Now.AddDays(-1*r).Date).
Where(Function(r) r.DayOfWeek = DayOfWeek.Monday).FirstOrDefault()
Ramil Aliyev 007
  • 4,437
  • 2
  • 31
  • 47
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jan 23 '22 at 01:06