Returns a the last day of the week for a given date. - CSharp System

CSharp examples for System:DateTime Day

Description

Returns a the last day of the week for a given date.

Demo Code


using System.Globalization;
using System.Windows.Forms;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from ww w  . j  av a 2s  .c  o m

public class Main{
        // -----------------------------------------------------------------------
        // GetLastDOW
        // Returns a the last day of the week for a given date. 
        // In this routine, Sunday is considered to be the last day of the 
        // However, the day considered to be the last day of the week may vary depending
        // upon culture. Cultural specific coding is not covered in this sample.
        // -----------------------------------------------------------------------
        public static DateTime GetLastDOW(DateTime dtDate)
        {
            DateTime LastDayofWeek = dtDate;
            //CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek
            switch (dtDate.DayOfWeek)
            {
                case DayOfWeek.Sunday:
                    LastDayofWeek = dtDate.AddDays(6);
                    break;
                case DayOfWeek.Monday:
                    LastDayofWeek = dtDate.AddDays(5);
                    break;
                case DayOfWeek.Tuesday:
                    LastDayofWeek = dtDate.AddDays(4);
                    break;
                case DayOfWeek.Wednesday:
                    LastDayofWeek = dtDate.AddDays(3);
                    break;
                case DayOfWeek.Thursday:
                    LastDayofWeek = dtDate.AddDays(2);
                    break;
                case DayOfWeek.Friday:
                    LastDayofWeek = dtDate.AddDays(1);
                    break;
                case DayOfWeek.Saturday:
                    LastDayofWeek = dtDate;
                    break;
            }

            return LastDayofWeek;

        }
}

Related Tutorials