top
User Name
Pass Word:

home
archives
features
links
users
faqs
registration!
Blatherings

This week's logic problem...
Previous | Next by ben 25 June, 2008 - 9:31 AM

I know Jamie will probably be the only one to want to answer this, and I don't want to screw up his vacation, but...

I need to subtract a number of days (X) from today, but ignore weekends. I think i'm going about it wrong, because I keep getting screwed up if i go past a week.

Example: today (wednesday) if i subtract 3 days, i want to have friday's date. If i subtract 4 days, i want to have last thursday, and 5 days, last wednesday, and so forth...

any ideas?




6/25/2008 >> muhgcee

What do you mean ignore weekends? You mean that you want to subtract a number of weekdays from today?

Or do you mean you want to subtract a number of days, but if you end up landing on a Saturday or Sunday that you want to say you landed on a Monday or Friday?


6/26/2008 >> elfie

Can you post what you have that works for less than a week? Also, safe to assume C#? Or SQL? Or what? Working with dates kinda needs a language to go on because there are different date functions available :)


6/26/2008 >> muhgcee

I think this will be using the modulus function.


6/27/2008 >> ben

stu - i mean that if you start on a monday (like the 23rd) and subtract 1 day, you want to land on friday the 20th... so, as if weekends didn't exist.

Jamie, i'll post what i had... this question is purely for our benefit now, as the requirement changed back to what i had originally been told (and coded)


6/27/2008 >> muhgcee

x = day to subtract from
y = days to subtract
z = x - y (this is a date)

I think all you have to do is...
if z == Saturday
{ z = z - 1 }
else if z == Sunday
{ z = z - 2}


6/27/2008 >> muhgcee

Oh but you need to ignore all weekends inbetween. Hmm...


6/27/2008 >> ben

this is the stuff i have so far, it's all C#, can't use the DB

protected void Page_Load(object sender, EventArgs e)
{
DateTime temp = DateTime.Today;
for (int i = 1; i < 31; i++)
{
DateTime output = temp.AddDays(i * -1);
//get # of weekend days to subtract
int startDayOfWeek = (int)temp.DayOfWeek;
int endDayOfWeek = (int)output.DayOfWeek;
int weekDays = Weekdays(output, temp);
Response.Write("weekdays: " + weekDays + ", ");
//int weekends = i > weekDays ? i - weekDays : 0;
//Response.Write("Weekends passed: " + weekends + ", ");
output = temp.AddDays(-1 * weekDays);
if (output.DayOfWeek == DayOfWeek.Saturday || output.DayOfWeek == DayOfWeek.Sunday)
output = output.AddDays(-2);
if (i < 7 && (enddayofweek > startdayofweek || enddayofweek == 0)) //we've gone over the weekend
output = output.adddays(-2);
if (i >= 7)
{
output = output.adddays(-2 * (i / 7));
if ((int)output.dayofweek == 0 || (int)output.dayofweek == 6)
output = output.adddays(-2);
}

//Response.Write("end day of week: " + endDayOfWeek);
Response.Write(", number of days subtracted: " + i);
Response.Write(", output day: " + output.ToShortDateString() + "
");
}
}

int Weekdays(DateTime dtmStart, DateTime dtmEnd)
{
// This function includes the start and end date in the count if they fall on a weekday
int dowStart = ((int)dtmStart.DayOfWeek == 0 ? 7 : (int)dtmStart.DayOfWeek);
int dowEnd = ((int)dtmEnd.DayOfWeek == 0 ? 7 : (int)dtmEnd.DayOfWeek);
TimeSpan tSpan = dtmEnd - dtmStart;
if (dowStart <= dowEnd)
{
return (((tSpan.Days / 7) * 5) + Math.Max((Math.Min((dowEnd + 1), 6) - dowStart), 0));
}
else
{
return (((tSpan.Days / 7) * 5) + Math.Min((dowEnd + 6) - Math.Min(dowStart, 6), 5));
}
}




You must be logged in to comment.

comments

links

www.flickr.com
This is a Flickr badge showing public photos from Kheiligh. Make your own badge here.

flickr