Is there a C# function which will give me the last day of the most recently finished Quarter given a date?
For example,
var lastDayOfLastQuarter = SomeFunction(jan 3, 2010);
would set lastDayOfLastQuarter = Dec 31, 2009
Is there a C# function which will give me the last day of the most recently finished Quarter given a date?
For example,
var lastDayOfLastQuarter = SomeFunction(jan 3, 2010);
would set lastDayOfLastQuarter = Dec 31, 2009
Assuming quarters always end at 3 month intervals you could do:
Maybe not the best solution, but very easy to read and modify compared to other solutions offered.
Here's a simple function to give you the last day of the current quarter (assuming you're using a standard calendar).
Hope that helps.
Usage:
This method has the advantage in that if you have an odd definition of quarters (for example, fiscal year is different than calendar year) the method
QuartersInYear
is easily modified to handle this.Try this:
The
AddMonths(-(d.Month-1) % 3))
moves the datevalue to the equivilent day of the first month in the quarter, and then theAddDays (-day)
moves back to the last day of the preceding month.A simple function can calculate the last days of the most recently completed month:
Test: