In c#, how to get the last day of next quarter giving the date?
For example, given date is 2014-12-02, I need to return the date 2015-03-31.
In c#, how to get the last day of next quarter giving the date?
For example, given date is 2014-12-02, I need to return the date 2015-03-31.
Very simple:
var given = new DateTime(2014, 02, 12);
var result =
given.Date
.AddDays(1 - given.Day)
.AddMonths(3 - (given.Month - 1) % 3)
.AddDays(-1);
//2014-03-31
If I input 2014-08-12 I get 2014-09-30.
Here are the results for the start of each month for the year:
Is that what you wanted?
Be carefull with this code! For example date 31.01.2018 will return 30.03.2018. This will work as expected:
var given = new DateTime(2018, 01, 31);
var result =
given.Date
.AddMonths(3 - (given.Month - 1) % 3);
result = result.AddDays(-result.Day);
//2018-03-31