Reputation: 37
I am trying t get last week's working date that is Monday and Sunday. The following syntax gives me the current week's Monday but not last week's.
DateTime thisMonday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Monday);
I am struggling to get last week's Monday's and Sunday's only dates and not the time.
Upvotes: 1
Views: 748
Reputation: 59208
not the time
DateTime.Today
only gives you a date at 0:00. DateTime.Now
gives you the same date including time.
I am struggling to get last week's Monday's and Sunday's
Just subtract 1 week ...
DateTime thisMonday = DateTime.Now.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Monday);
DateTime lastMonday = thisMonday.AddDays(-7);
Console.WriteLine(lastMonday);
If you dislike the magic number 7, you might use
Enum.GetNames(typeof(DayOfWeek)).Length
Upvotes: 2