Add Week Days - CSharp System

CSharp examples for System:DateTime Week

Description

Add Week Days

Demo Code


using System;//from w w  w  . j  av  a2  s  .  com

public class Main{
        public static DateTime AddWeekDays(this DateTime instance, TimeSpan timeSpan)
        {
            return AddWeekDays(instance, Convert.ToInt32(timeSpan.TotalDays));
        }
        public static DateTime AddWeekDays(this DateTime instance, int days)
        {
            var sign = Math.Sign(days);
            var unsignedDays = Math.Abs(days);
            for (var i = 0; i < unsignedDays; i++)
            {
                do
                {
                    instance = instance.AddDays(sign);
                } while (instance.IsWeekEnd());
            }
            return instance;
        }
        public static bool IsWeekEnd(this DateTime instance)
        {
            return instance.DayOfWeek == DayOfWeek.Saturday ||
                   instance.DayOfWeek == DayOfWeek.Sunday;
        }
}

Related Tutorials