Pooja
Pooja

Reputation: 37

Get last week's dates only for Monday and Sunday

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

Answers (1)

Thomas Weller
Thomas Weller

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

Related Questions