How to determine if a date is between the dates of the current week?

In C #,

How do we check a specific date with dates of the week?

For example: 02/06/2014

Current weeks: 02/02/2014 - 02/08/2014

so that these dates are next week ....

+1
source share
4 answers

Use this for verification (the last parameter is optional, if you always want 1 week from fromDate, you don't need the last parameter):

 public static bool DateInside(DateTime checkDate, 
     DateTime fromDate, DateTime? lastDate = null)
 {
     DateTime toDate = lastDate != null ? lastDate.Value : fromDate.AddDays(6d);
     return checkDate >= fromDate && checkDate <= toDate;
 }

To call, use:

bool isDateInside = DateInside(new DateTime(2014, 02, 06), 
     new DateTime(2014, 02, 02)); // return true

And find the first option :) The answer is also here: How to check if C # DateTime is within range

If you want to check if the dates are in the same week , you can use this:

public static bool DateInsideOneWeek(DateTime checkDate, DateTime referenceDate)
{
    // get first day of week from your actual culture info, 
    DayOfWeek firstWeekDay = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
    // or you can set exactly what you want: firstWeekDay = DayOfWeek.Monday;
    // calculate first day of week from your reference date
    DateTime startDateOfWeek = referenceDate;
    while(startDateOfWeek.DayOfWeek != firstWeekDay)
    { startDateOfWeek = startDateOfWeek.AddDays(-1d); }
    // fist day of week is find, then find last day of reference week
    DateTime endDateOfWeek = startDateOfWeek.AddDays(6d);
    // and check if checkDate is inside this period
    return checkDate >= startDateOfWeek && checkDate <= endDateOfWeek;
}

, 3 2014 ( 3 9 ). ( ), (2014--06), :

For 2014-Feb-02 (Sunday before this week): false
For 2014-Feb-03 (Monday inside this week): true
For 2014-Feb-06 (Today  inside this week): true
For 2014-Feb-09 (Sunday inside this week): true
For 2014-Feb-10 (Monday next        week): false

, , , :

DateInsideOneWeek(new DateTime(2014, 02, 02), new DateTime(2014, 02, 06));

:

DateTime startDateOfWeek = DateTime.Now.Date; // start with actual date
while(startDateOfWeek.DayOfWeek != DayOfWeek.Monday) // set first day of week in your country
{ startDateOfWeek = startDateOfWeek.AddDays(-1d); } // after this while loop you get first day of actual week
DateTime endDateOfWeek = startDateOfWeek.AddDays(6d); // you just find last week day

?

+2

hmm

public bool isBetween(DateTime input, DateTime date1, DateTime date2)
    {
        if (input > date1 && input < date2)
            return true;
        else
            return false;
    }

?

input = date1 date2 =

+1

:

bool inRange = (date >= lowerDate && date <= upperDate);
0

:)

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;
    }
}

Update: it seems to have at least twice as good performance as @Atiris :)

0
source

All Articles