More elegant way to get "Friday of last week"?

S

sherifffruitfly

Hi all,

This is how I'm currently getting Friday of last week. It strikes me as
cumbersome. Is there a slicker/more elegant way?

Thanks for any ideas,

cdj



private string prevFridayString(DateTime day)
{
DateTime rv;
int offset = 0;

switch (day.DayOfWeek)
{
case DayOfWeek.Sunday:
offset = 5;
break;
case DayOfWeek.Monday:
offset = 4;
break;
case DayOfWeek.Tuesday:
offset = 3;
break;
case DayOfWeek.Wednesday:
offset = 2;
break;
case DayOfWeek.Thursday:
offset = 1;
break;
case DayOfWeek.Friday:
offset = 0;
break;
case DayOfWeek.Saturday:
offset = -1;
break;
}

rv = day.AddDays(-7 + offset);
return rv.ToShortDateString();
}
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

sherifffruitfly said:
This is how I'm currently getting Friday of last week. It strikes me as
cumbersome. Is there a slicker/more elegant way?

Something like:

DateTime dt = DateTime.Now;
while(dt.DayOfWeek != DayOfWeek.friday) dt = dt.AddDays(-1);

[untested - please check]

Arne
 
S

sherifffruitfly

Arne said:
DateTime dt = DateTime.Now;
while(dt.DayOfWeek != DayOfWeek.friday) dt = dt.AddDays(-1);

Nice. The idea is perfectly clear - thanks!

cdj
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

sherifffruitfly said:
Nice. The idea is perfectly clear - thanks!

If you want to go 7 days back if today is Friday then you will need:

DateTime dt = DateTime.Now.AddDays(-1);

You should also note that it is not optimized for speed.

But any maintenance programmer reading the code will understand
the "algorithm".

Arne
 
N

nick.fletcher

You can achieve this quite easily using recurssion

public partial class Form1 : Form
{

private DateTime lastFriday;

public Form1()
{
InitializeComponent();
}

private void monthCalendar1_DateSelected(object sender,
DateRangeEventArgs e)
{
selectedText.Text = e.Start.ToShortDateString();
GetLastFriday(e.Start);
lastFridayText.Text = lastFriday.ToShortDateString();
}

private void GetLastFriday(DateTime dateTime)
{
dateTime = dateTime.Subtract(new TimeSpan(1,0,0,0));
if (dateTime.DayOfWeek == DayOfWeek.Friday)
{
lastFriday = dateTime;
return;
}
else
GetLastFriday(dateTime);
}
}

Remember to add a check so your method doesnt execute more than 7 times
(days in a week) or you could get a stack overflow if things go wrong
 
P

Peter Duniho

You can achieve this quite easily using recurssion

Yes, you can. And it's a classic example of a problem that *shouldn't* be
done using recursion.

Don't use recursion when a simple loop will suffice. Recursion is great if
you have a need to unwind all of your state. It's a complete waste of
coding effort, stack space, and execution time when one never needs to
actually revisit previous results, as is the case here.

Pete
 
S

Stephany Young

Did everyone miss?:

private string prevFridayString(DateTime day)
{
return day.AddDays(-((int)day.DayOfWeek) + 2)).ToShortDateString();
}

Remember that the underlying values for the DayOfWeek enumeration are int's
with Sunday being 0 and Saturday being 6.

Therfore is is simply a matter of subtracting the value of (day.DayOfWeek
plus 2).
 
P

PS

You can achieve this quite easily using recurssion

public partial class Form1 : Form
{

private DateTime lastFriday;

public Form1()
{
InitializeComponent();
}

private void monthCalendar1_DateSelected(object sender,
DateRangeEventArgs e)
{
selectedText.Text = e.Start.ToShortDateString();
GetLastFriday(e.Start);
lastFridayText.Text = lastFriday.ToShortDateString();
}

private void GetLastFriday(DateTime dateTime)
{
dateTime = dateTime.Subtract(new TimeSpan(1,0,0,0));
if (dateTime.DayOfWeek == DayOfWeek.Friday)
{
lastFriday = dateTime;
return;
}
else
GetLastFriday(dateTime);
}
}

Remember to add a check so your method doesnt execute more than 7 times
(days in a week) or you could get a stack overflow if things go wrong

Not only is it unnecessarily complex it is also incorrect. If you started
with Saturday it would return the Friday from the same week not "Friday Of
Last Week".

PS
 
J

Jeff Louie

Just for fun, here is the actual algorithm in C++ based on Zeller and
yes 0 for Sunday is how it was done:

GetFirstDayInMonth
// Days since Sunday 0-6
// Based on Zeller
// ASSERT Year>1, Month>=1 && <=12.
int CCalendar::GetFirstDayInMonth(unsigned int Year, unsigned int Month)
{
const int day=1;
if (Month < 3) {
Month +=12;
Year -= 1;
}
return
((day+1+(Month*2)+(int)((Month+1)*3/5)+Year+(int)(Year/4)-(int)(Year/100
)+(int)(Year/400))%7);
}

Regards,
Jeff
 
S

sherifffruitfly

Jeff said:
Just for fun, here is the actual algorithm in C++ based on Zeller and
yes 0 for Sunday is how it was done:

Wow - this turned into a whole lot more "fun" than I was expecting! I
especially love the recursive solution - lolol!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top