Add Weekdays - CSharp System

CSharp examples for System:DateTime Week

Description

Add Weekdays

Demo Code


using System.Threading.Tasks;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System;/*from  w ww.  j  a v a2  s  .  c o m*/

public class Main{
        public static DateTime AddWeekdays(this DateTime date, int days)
        {
            var sign = days < 0 ? -1 : 1;
            var unsignedDays = Math.Abs(days);
            var weekdaysAdded = 0;
            while (weekdaysAdded < unsignedDays)
            {
                date = date.AddDays(sign);
                if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
                    weekdaysAdded++;
            }
            return date;
        }
}

Related Tutorials