Question

I know I can calculate days difference between two dates by using this:

private int DaysFromStart(DateTime date, DateTime startDate)
{
    var days =  date.Subtract(startDate);
    return (int)days.TotalDays;
}

but now i want to get the difference between two dates by only include weekdays. So for example, if the start date was on Friday and the date to check was the following Monday then the return value would be 1.

Était-ce utile?

La solution

Consider the following code snippet...

int allDays = (int)date.Subtract(startDate).TotalDays;
int weekDays = Enumerable
                .Range(1, allDays)
                .Select(day => startDate.AddDays(day))
                .Count(day => day.DayOfWeek != DayOfWeek.Saturday && day.DayOfWeek != DayOfWeek.Sunday);

Good Luck!

Autres conseils

This will give you the remaider days left(excluding sunday and saturday).The variables start and end represent the start and end dates:

int DaysLeft = 0;
for (int i = 1;i <= (int)end.Subtract(start).TotalDays;i++)
{
    DayOfWeek day = start.AddDays(i).DayOfWeek;
    DaysLeft = (day == DayOfWeek.Sunday) || (day == DayOfWeek.Saturday)?DaysLeft:DaysLeft + 1;
}

I used an extension method to return the amount of weekdays in a time span, or even the full list of days between two dates, then you can run a lambda expression against the IEnumerable to get what you need. Here is a very basic example of the static class:

public static class TimeSpanextensions
{
    public static IEnumerable<DateTime> GetDays(this DateTime start, DateTime end, bool returnWeekDays = false)
    {
        List<DateTime> daysInSpan = new List<DateTime>();
        //change following line to accomodate date format if needed
        TimeSpan ts = start.Date - end.Date;

        if(start < end)
        {
            DateTime _this = start;

            while (_this < end) 
            {
                _this = start.AddDays(1);
                daysInSpan.Add(_this);
            }
        }

        if(start > end)
        {
            DateTime _this = start;
            while(_this > end)
            {
                _this = _this.AddDays(-1);
                daysInSpan.Add(_this);
            }
        }

        if(returnWeekDays)
        {
            return daysInSpan.Where(d => d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday);
        }

        return daysInSpan;
    }

I used this with the following example:

public static IEnumerable<DateTime> WeekDays(DateTime one, DateTime two)
    {
        TimeSpan test = one - two;

        return one.GetDays(two, true).ToList();
    }

This is a basic example to return the days or weekdays of a timespan as an actual working type, instead of the TimeSpan days of type int that TimeSpan returns. SO if you have two dates StartDate and EndDate, you would then just do the following to get the weekdays that exist in the TimeSpane.

   int count = Start < EndDate ? 1 : -1;
   DateTime newDate = StartDate;
   List<DateTime> allDaysList = new List<DateTime();

   while(newDate != EndDate)
   {

       newDate = newDate.AddDays(count); //will eithr add or subtract a day
       allDays.Add(newDate);           
   }


   //now return either only weekdays days
   allDays = allDays.Where(d => d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday);

And if you just want the count of weekdays in a TimeSpan do the following:

 int workDays =  allDays.Where(d => d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday).Count();

That is different ways to handle this problem. To Extend this you just create a static method that excepts this as a argument. For example:

List<DateTime> weekDaysOnly = StartsDate.GetDays(endDate).ToList();
//then once again do your business day lambda expression here to leave your list with weekdays only. Hope this helps.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top