0

Hi I am able to get the first day of the month using this code:

static string FirstMonth = DateTime.Today.AddDays(1 - DateTime.Today.Day).ToString("yyyy-MM-dd");

How do I get the last day of the month in the same date format?

Thanks

Billy Hen
  • 13
  • 8

1 Answers1

1
DateTime d = Convert.ToDateTime("2/4/15");

DateTime lastDayOfSameMonth = (new DateTime(d.Year, d.Month, 1)).AddMonths(1).AddDays(-1);
danyolgiax
  • 12,798
  • 10
  • 65
  • 116
Marc Johnston
  • 1,276
  • 1
  • 7
  • 16
  • 1
    Please, use [format code](http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks) option while creating your answers. It will make the posts look much better. It is also a good idea to add some explanation and, possibly, references to the documentation. That is not a requirement, but pleasantly looking posts with good formatting, explanations and references are far more likely to get upvotes and(or) be accepted. – Eugene Podskal May 13 '15 at 16:48