Date Range in C#

Get Date in Week:

public static DateTime EndOfWeek(DateTime dateTime)
        DateTime start = StartOfWeek(dateTime);

        return start.AddDays(6);

    public static DateTime StartOfWeek(DateTime dateTime)
        int days = dateTime.DayOfWeek - DayOfWeek.Monday; 

        if (days < 0) 
            days += 7;

        return dateTime.AddDays(-1 * days).Date;

Get Date in Month:

public DateTime FirstDayOfMonthFromDateTime(DateTime dateTime)
   return new DateTime(dateTime.Year, dateTime.Month, 1);
public DateTime LastDayOfMonthFromDateTime(DateTime dateTime)
   DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);
   return firstDayOfTheMonth.AddMonths(1).AddDays(-1);

Referenced by:

Categories: C#

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s