First Day Of Month - CSharp System

CSharp examples for System:DateTime Month

Description

First Day Of Month

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from  ww  w  .j a v a 2 s .  c  o  m

public class Main{
        public static DateTime FirstDayOfMonth(this DateTime dt, DayOfWeek dow)
        {
            return dt.FirstDayOfMonth().NextDay(dow, true);
        }
        public static DateTime FirstDayOfMonth(this DateTime dt)
        {
            return new DateTime(dt.Year, dt.Month, 1);
        }
        public static DateTime NextDay(this DateTime dt, DayOfWeek dow, bool includeThis)
        {
            int diff = dow - dt.DayOfWeek;
            if ((includeThis && diff < 0) || (!includeThis && diff <= 0)) diff += 7;
            return dt.Date.AddDays(diff);
        }
        public static DateTime NextDay(this DateTime dt, DayOfWeek dow)
        {
            return dt.NextDay(dow, false);
        }
        public static DateTime NextDay(this DateTime dt)
        {
            return dt.Date.AddDays(1);
        }
}

Related Tutorials