C # find the same range of "named days" from 1 year ago

using C # visual studio 2008.

Can anyone help with an algorithm for this please

If I have a range of days selected this week (e.g. Monday to Friday), I can find the dates for them using the available datetime functions.

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

So I need to go back for 1 year and find the dates for the nearest Mon to Day from 1 year ago. I guess I also need to consider the years of leap development.

Can anyone help with a suitable algorithm on how to achieve this. Of course, the DAY for today in the past year will not be the same day.

early

+3
source share
5 answers

Here is the code that can do what you want - but test cases show that there are angular cases:

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 like to receive for the second call? This code is returned on January 1, 2010, because the nearest date is "a year ago on the same day."

I highly recommend that you have everything, you have unit tests that check leap years, the beginning and end of the year, etc.

+1
source

Suppose you select Wednesday 10-02-2010 - Friday 12-02-2010 this year. Last year it would be Tuesday 10-02-2009 - Thursday 12-02-2009.

So, you can do the following: return for a year by simply doing DateTime.AddYears (-1). Make sure you recuperate during the leap years here.

.AddDays(1), -. , , , .

0

I just subtracted one year, then ran back until I found Monday. LastYear will eventually be 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);
0
source
    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 the datetime object for the last year, and then use the DayOfWeek property.

0
source

It was fun.

        // today 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 info 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 highly recommend using the module testing features built into VS2008 to make sure you consider corner cases.

0
source

Source: https://habr.com/ru/post/1732132/


All Articles