Check if the date-time matches the same week as another time

Say I have a list of dates in a table. Now I want to find all rows that are in the same week as the date provided as an argument.

Let's say I have a table with:

  • 2014-09-11
  • 2014-09-12
  • 2014-09-15

And I pass this function to argument 2014-09-09, it should look from Monday to Sundays and understand that 09-11 and 09-12 are part of the week, but not 09-15.

How to solve this problem?

I thought about checking for a year, month, and week, but you cannot guarantee that the month is the same ...

So how do you solve such a problem?

+4
source share
7 answers

DateTime.Year Calendar.GetWeekOfYear(DateTime,...). .

+7

. DxCk . , , .

: , . :

    private bool DatesAreInTheSameWeek(DateTime date1, DateTime date2)
    {
        var cal = System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar;
        var d1 = date1.Date.AddDays(-1 * (int)cal.GetDayOfWeek(date1));
        var d2 = date2.Date.AddDays(-1 * (int)cal.GetDayOfWeek(date2));

        return d1 == d2;
    }
+4

: public virtual int GetWeekOfYear(DateTime time,CalendarWeekRule rule,DayOfWeek firstDayOfWeek) Calendar class

+2

?

bool AreFallingInSameWeek(DateTime date1, DateTime date2)
{
    return date1.AddDays(-(int)date1.DayOfWeek) == date2.AddDays(-(int)date2.DayOfWeek);
}

, ,

bool AreFallingInSameWeek(DateTime date1, DateTime date2, DayOfWeek weekStartsOn)
{
    return date1.AddDays(-GetOffsetedDayofWeek(date1.DayOfWeek, (int)weekStartsOn)) == date2.AddDays(-GetOffsetedDayofWeek(date2.DayOfWeek, (int)weekStartsOn));
}

int GetOffsetedDayofWeek(DayOfWeek dayOfWeek, int offsetBy)
{
    return (((int)dayOfWeek - offsetBy + 7) % 7)
}
+2

, DOB, . , . :

  • DOB (, 24-02-1988 24-02-2018 ( ).
  • , dec, jan
  • .
  • .
  • , ​​ ​​ .

    private bool DatesAreInTheSameWeek(DateTime birthday)
    {
        if (birthday == DateTime.MinValue)
        {
            return false;
        }
        else
        {
            var birtdayWithCurrentYear = new DateTime(DateTime.Today.Year, birthday.Month, birthday.Day);
            if (birthday.Month == 1 && DateTime.Today.Month != 1)
            {
                birtdayWithCurrentYear = birtdayWithCurrentYear.AddYears(1);
            }
            DateTime firstDayInWeek = DateTime.Today.Date;
            while (firstDayInWeek.DayOfWeek != CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)
                firstDayInWeek = firstDayInWeek.AddDays(-1);var lastDayInWeek = firstDayInWeek.AddDays(7);
    
            return birtdayWithCurrentYear < lastDayInWeek && birtdayWithCurrentYear >= firstDayInWeek;
    
        }
    }
    
+1

Calendar, :

public static int WeekOfYear(DateTime dt)
{
    int startDays = 0;
    // first day of the year
    DateTime firstJanuary = new DateTime(dt.Year, 1, 1);

    if (firstJanuary.DayOfWeek == DayOfWeek.Tuesday)
    {
        startDays = 1;
    } 
    else if (firstJanuary.DayOfWeek == DayOfWeek.Wednesday)
    {
        startDays = 2;
    }
    else if (firstJanuary.DayOfWeek == DayOfWeek.Thursday)
    {
        startDays = 3;
    }
    else if (firstJanuary.DayOfWeek == DayOfWeek.Friday)
    {
        startDays = 4;
    }
    else if (firstJanuary.DayOfWeek == DayOfWeek.Saturday)
    {
        startDays = 5;
    }
    else if (firstJanuary.DayOfWeek == DayOfWeek.Sunday)
    {
        startDays = 6;
    }

    if (DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek == DayOfWeek.Sunday)
    {
        startDays++;
        startDays = startDays % 7;
    }

    return ((dt.DayOfYear + startDays - 1) / 7) + 1;
}
0

@DxCK, , :

public static class DateExtensions
{
    private static void Swap<T>(ref T one, ref T two)
    {
        var temp = one;
        one = two;
        two = temp;
    }

    public static bool IsFromSameWeek(this DateTime first, DateTime second, DayOfWeek firstDayOfWeek = DayOfWeek.Monday)
    {
        // sort dates
        if (first > second)
        {
            Swap(ref first, ref second);
        }

        var daysDiff = (second - first).TotalDays;
        if (daysDiff >= 7)
        {
            return false;
        }

        const int TotalDaysInWeek = 7;
        var adjustedDayOfWeekFirst = (int)first.DayOfWeek + (first.DayOfWeek < firstDayOfWeek ? TotalDaysInWeek : 0);
        var adjustedDayOfWeekSecond = (int)second.DayOfWeek + (second.DayOfWeek < firstDayOfWeek ? TotalDaysInWeek : 0);

        return adjustedDayOfWeekSecond >= adjustedDayOfWeekFirst;
    }
}

Also here is a link to another correct solution with a slightly different approach.

0
source

All Articles