469,275 Members | 1,649 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

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

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();
}

Nov 16 '06 #1
10 30346
sherifffruitfly wrote:
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
Nov 16 '06 #2

Arne Vajhøj wrote:
>
DateTime dt = DateTime.Now;
while(dt.DayOfWeek != DayOfWeek.friday) dt = dt.AddDays(-1);
Nice. The idea is perfectly clear - thanks!

cdj

Nov 16 '06 #3
sherifffruitfly wrote:
Arne Vajhøj wrote:
>DateTime dt = DateTime.Now;
while(dt.DayOfWeek != DayOfWeek.friday) dt = dt.AddDays(-1);

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
Nov 16 '06 #4
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

On Nov 16, 1:14 pm, "sherifffruitfly" <sherifffruit...@gmail.com>
wrote:
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();
}
Nov 16 '06 #5
<ni***********@iinet.net.auwrote in message
news:11**********************@h54g2000cwb.googlegr oups.com...
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
Nov 16 '06 #6
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).
"sherifffruitfly" <sh*************@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
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();
}

Nov 16 '06 #7
PS

<ni***********@iinet.net.auwrote in message
news:11**********************@h54g2000cwb.googlegr oups.com...
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
>
On Nov 16, 1:14 pm, "sherifffruitfly" <sherifffruit...@gmail.com>
wrote:
>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();
}
Nov 16 '06 #8
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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 16 '06 #9

Jeff Louie wrote:
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!

Nov 16 '06 #10

Arne Vajhøj wrote:
sherifffruitfly wrote:
Arne Vajhøj wrote:
DateTime dt = DateTime.Now;
while(dt.DayOfWeek != DayOfWeek.friday) dt = dt.AddDays(-1);
Nice. The idea is perfectly clear - thanks!

If you want to go 7 days back if today is Friday then you will need:
Gah! Killed the one-liner, eh? lol!

thanks again,

cdj

Nov 16 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

24 posts views Thread by Hardy | last post: by
388 posts views Thread by maniac | last post: by
10 posts views Thread by craig.keightley | last post: by
3 posts views Thread by sbaird | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.