473,372 Members | 1,073 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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 32663
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: strutsng | last post by:
<input type="file"> only allows the user to browse for files. How about "browse for folder" dialog? Can html/javascript do that? I couldn't find any syntax for that. If not, please advise what...
24
by: Hardy | last post by:
I'm pretty new in this field. when reading some 70x material, I met with this term but cannot catch its accurate meaning. who can help me? thanks in advance:)~
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
1
by: Chris | last post by:
I built small C# Web and Web Service applications in a training class last week. The applications worked in the class, but when I tried to run them again over the weekend, they both bombed....
32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
4
by: trint | last post by:
Ok, I received info that this will work as a means of removing the border around a window that I create (which also loads an aspx file) in the firing file (aspx using javascript): OpenWindow =...
10
by: craig.keightley | last post by:
I am trying to get the next row within a loop for a script i am developing... I need to display a final table row within the table that i have displayed on the page, but i only want to show it...
3
by: sbaird | last post by:
Aloha from Hawaii, I'm beating my head on the wall here. I have a recruiting contact managment database I'm trying to create. Managers (there ar 14 of them) have to make a certain number of...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.