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

CSharp examples for System:DateTime Day

Description

Returns a the first 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;// w  ww  .  j a v  a 2s  .com

public class Main{
        // -----------------------------------------------------------------------
        // GetFirstDOW
        // Returns a the first day of the week for a given date. 
        // In this routine, Sunday is considered to be the first day of the 
        // However, the day considered to be the first day of the week may vary depending
        // upon culture. Cultural specific coding is not covered in this sample
        // -----------------------------------------------------------------------
        public static DateTime GetFirstDOW(DateTime dtDate)
        {
            DateTime FirstDayofWeek = dtDate;

           //CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek
            switch (dtDate.DayOfWeek)            
            {
                case DayOfWeek.Sunday:
                    FirstDayofWeek = dtDate;
                    break;
                case DayOfWeek.Monday:
                    FirstDayofWeek = dtDate.AddDays(-1);
                    break;
                case DayOfWeek.Tuesday:
                    FirstDayofWeek = dtDate.AddDays(-2);
                    break;
                case DayOfWeek.Wednesday:
                    FirstDayofWeek = dtDate.AddDays(-3);
                    break;
                case DayOfWeek.Thursday:
                    FirstDayofWeek = dtDate.AddDays(-4);
                    break;
                case DayOfWeek.Friday:
                    FirstDayofWeek = dtDate.AddDays(-5);
                    break;
                case DayOfWeek.Saturday:
                    FirstDayofWeek = dtDate.AddDays(-6);
                    break;

            }
            return FirstDayofWeek;

        }
}

Related Tutorials