To Week End - CSharp System

CSharp examples for System:DateTime Week

Description

To Week End

Demo Code


using System;/*from  www .  j a v  a 2  s  .  c om*/

public class Main{
        private static DateTime ToWeekEnd(this DateTime date, DayOfWeek week)
        {
            DateTime WeekStart = new DateTime();
            switch (week)
            {
                case DayOfWeek.Monday:
                    WeekStart = date.AddDays(6);
                    break;
                case DayOfWeek.Tuesday:
                    WeekStart = date.AddDays(5);
                    break;
                case DayOfWeek.Wednesday:
                    WeekStart = date.AddDays(4);
                    break;
                case DayOfWeek.Thursday:
                    WeekStart = date.AddDays(3);
                    break;
                case DayOfWeek.Friday:
                    WeekStart = date.AddDays(2);
                    break;
                case DayOfWeek.Saturday:
                    WeekStart = date.AddDays(1);
                    break;
                case DayOfWeek.Sunday:
                    WeekStart = date;
                    break;
            }
            return WeekStart;
        }
        public static DateTime ToWeekEnd(this DateTime value)
        {
            DateTime weekEnd = ToWeekEnd(value, value.Date.DayOfWeek);
            string shortDate = weekEnd.ToShortDateString();
            return Convert.ToDateTime(shortDate).AddDays(1).AddSeconds(-1);
        }
        public static DateTime ToWeekEnd(this string value)
        {
            DateTime date = Convert.ToDateTime(value);
            DateTime weekEnd = ToWeekEnd(date, date.Date.DayOfWeek);
            string shortDate = weekEnd.ToShortDateString();
            return Convert.ToDateTime(shortDate).AddDays(1).AddSeconds(-1);
        }
}

Related Tutorials