IntroductionIn my project, I required to cache data based on the no of days by start and end of the month, so I have to take the first / last days in month using following snippet. here those for you as well. Code using System;using System.Collections.Generic;using System.Text;namespace WindowsApplication1
{public class DateTimeUtility{public static DateTime GetFirstDayOfMonth(DateTime dtDate){DateTime dtFrom = dtDate;dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1));return dtFrom;}public static DateTime GetFirstDayOfMonth(int Month){DateTime dtFrom = new DateTime(DateTime.Now.Year, Month, 1);return GetFirstDayOfMonth(dtFrom);}public static int GetNoOfDaysinMonth(int year, int month){return DateTime.DaysInMonth(year, month);}public static int GetNoOfDaysinMonth(DateTime dtDate){return GetNoOfDaysinMonth(dtDate.Year, dtDate.Month);}public static DateTime GetLastDayOfMonth(DateTime dtDate){DateTime dtTo = dtDate;dtTo = dtTo.AddMonths(1);dtTo = dtTo.AddDays(-(dtTo.Day));return dtTo;}public static DateTime GetLastDayOfMonth(int month){DateTime dtTo = new DateTime(DateTime.Now.Year, month, 1);return GetLastDayOfMonth(dtTo);}}} Hope help this you as well. |