Calculating the last day of a month in .NET

I had a drop down list which allowed the user to select a month and year, such as “December 2015”. The value of the DLL was the start date (“12/1/2015”), but I needed to generate the end date based on the start date. So here is the little method I wrote to do that, which uses the DateTime.DaysInMonth method to get the totals days of the selected month:

private DateTime CalculateEndDate(DateTime startDate) {
    return new DateTime(startDate.Year, startDate.Month, DateTime.DaysInMonth(startDate.Year, startDate.Month));
}

 

 

Advertisements
  1. Leave a comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: