c# get last day of month code example

Example 1: get what week of the month c#

static int GetWeekNumberOfMonth(DateTime date)
{
    date = date.Date;
    DateTime firstMonthDay = new DateTime(date.Year, date.Month, 1);
    DateTime firstMonthMonday = firstMonthDay.AddDays((DayOfWeek.Monday + 7 - firstMonthDay.DayOfWeek) % 7);
    if (firstMonthMonday > date)
    {
        firstMonthDay = firstMonthDay.AddMonths(-1);
        firstMonthMonday = firstMonthDay.AddDays((DayOfWeek.Monday + 7 - firstMonthDay.DayOfWeek) % 7);
    }
    return (date - firstMonthMonday).Days / 7 + 1;
}

Example 2: c# get last day of month

DateTime.DaysInMonth(1980, 08);

Example 3: get day month year from date c#

// can add : or / to seperate dates and time
DateTime.Now.ToString("yyyyMMddhhmmss")

Tags:

Misc Example