views:

236

answers:

5

using c# visual studio 2008.

Can anyone help with an algorithm to do this please

if i have a range of days selected for this week (eg monday to friday) i can find the dates for these using the datetime functions available.

What i want to do is compared to stored data for the same DAY range 1 year ago.

So basicly i need to go back 1 year and find the dates for the nearest Mon to fri DAY range from 1 year previous. I guess i also need to take into acount leap years.

Can anyone help with a suitable algorithm on how to achieve this. Of course the DAY for todays date last year is not going to be the same day.

thanks in advance

A: 

Let's say you select Wednesday 10-02-2010 - Friday 12-02-2010 this year. Last year that would have been Tuesday 10-02-2009 - Thursday 12-02-2009.

So you can do the following: Go back a year by simply performing DateTime.AddYears(-1). Make sure you correct for leap years here.

Then you use .AddDays(1) until you end up on a Wednesday - Friday timeframe. That way you only have to take leap years into account at one point and this should produce the result you need.

Anton
A: 

I just subtracted one year then ran backwards until I found a Monday. LastYear will end up being the first Monday before this date last year

DateTime LastYear = DateTime.Now.AddYears(-1)
DayOfWeek Check = LastYear.DayOfWeek;
while (Check != DayOfWeek.Monday)
{
    LastYear = LastYear.addDays(-1);
    Check = LastYear.DayOfWeek;
}
Console.WriteLine("{0}",LastYear);
rerun
+1  A: 

Here's some code which might do what you want - but the test cases show that there are corner cases to consider:

using System;

public class Test
{
    static void Main()
    {
        Console.WriteLine(SameDayLastYear(DateTime.Today));
        Console.WriteLine(SameDayLastYear(new DateTime(2010, 12, 31)));
    }

    static DateTime SameDayLastYear(DateTime original)
    {
        DateTime sameDate = original.AddYears(-1);
        int daysDiff = original.DayOfWeek - sameDate.DayOfWeek;
        return sameDate.AddDays(daysDiff);
    }
}

What would you want the result for the second call to be? This code returns January 1st 2010, because that's the closest date to "a year ago on the same day".

I strongly suggest that whatever you go with, you have unit tests checking leap years, start and end of year etc.

Jon Skeet
A: 
    DateTime now = DateTime.Now;
    DateTime lastyear = now.AddYears(-1);

    string dayOfWeek = lastyear.DayOfWeek.ToString();

    if (dayOfWeek.Equals("Saturday")) { dayOfWeek = "Friday"; }
    else if (dayOfWeek.Equals("Sunday")) { dayOfWeek = "Monday"; }

    Console.WriteLine(dayOfWeek);
    Console.ReadKey();

Get a datetime object for last year, then use the DayOfWeek property.

Russell
A: 

This was pretty fun.

        // today's info
        DateTime today = DateTime.Now;
        DayOfWeek today_name = today.DayOfWeek;
        // this day one year ago
        DateTime year_ago = today - new TimeSpan( ((today.Year - 1) % 4) ? 365 : 366, 0, 0, 0);
        // find the closest day to today's info's name
        DayOfWeek today_name_a_year_ago = year_ago.DayOfWeek;
        DateTime current_range_a_year_ago = year_ago - new TimeSpan( year_ago.DayOfWeek - today_name, 0, 0, 0);
        Console.WriteLine( "Today is {0}, {1}", today_name, today);
        Console.WriteLine( "One year from today was {0}, {1}", today_name_a_year_ago, year_ago);
        Console.WriteLine( "New date range is {0}", current_range_a_year_ago);

I would highly recommend using the unit testing features built into VS2008 to make sure you account for corner cases.

Dave
This assumes last year was not a leap year.
Russell
true! I actually am not sure why I had said that TimeSpan would account for the leap year, since I was subtracting days and not years. I'll try to edit the code properly.
Dave