0

I need to retrieve the date, month, and year of the last Tuesday relative to any given date. For example, today is Friday, 1st March 2013. I want my method to return the date of the prior Tuesday: 26th February, 2013. How can I achieve this?

Blumer
  • 5,005
  • 2
  • 33
  • 47
Sameek Kundu
  • 147
  • 2
  • 14
  • I haven't tried this, as I do not know how to achieve this. My input will be today's date , Datetime.Now, and I require the last tuesday's info as output – Sameek Kundu Mar 01 '13 at 17:29
  • So google is not working, then for a start point you may read [ASP.net get the next tuesday](http://stackoverflow.com/questions/6346119/asp-net-get-the-next-tuesday) – Mahmut Ali ÖZKURAN Mar 01 '13 at 17:36

3 Answers3

4

This should do the trick.

var yesterday = DateTime.Now;

while(yesterday.DayOfWeek != DayOfWeek.Tuesday) {
  yesterday = yesterday.AddDays(-1);
}
bmavity
  • 2,477
  • 2
  • 24
  • 23
0

I'd do something like this:

var lastTuesday = DateTime.Today.AddDays(
     -1 * (DateTime.Today.DayOfWeek - DayOfWeek.Tuesday));  
var lastMonday = DateTime.Today.AddDays(
     -1 * (DateTime.Today.DayOfWeek - DayOfWeek.Monday));
JerKimball
  • 16,584
  • 3
  • 43
  • 55
0

This was essentially answered here: Get date of first Monday of the week?

DateTime input = DateTime.Now;
int delta = DayOfWeek.Tuesday - input.DayOfWeek;
DateTime tuesday = input.AddDays(delta);
Community
  • 1
  • 1
Tim
  • 857
  • 6
  • 13