0

A person enters a date into the system, is there he selects February 28th, it reproduces march 28th. But my goal is to grab end of the month, March 31st. Is there a way to move to end of month for next month without incorporating the snippet below, in other words a cleaner method?

if (Date.Month == 2)
    Date.AddDays(31)
else if (Date.Month == 3)
    Date.AddDays(30)

etc...

any tips or suggestions would be great, clean code is always the best code.

Master
  • 2,038
  • 2
  • 27
  • 77

1 Answers1

0
var d = new DateTime(...);
var newDate = new DateTime(d.Year, d.Month, 1).AddMonths(2).AddDays(-1);

this should also work:

var newDate = new DateTime(d.Year,1,31).AddMonths(d.Month-1);
Robert McKee
  • 21,305
  • 1
  • 43
  • 57