Does anyone know how to construct a DateTime in .NET to be the first day of the current quarter?
If the month of an input date lies as above I need the output for in terms of Quarter number. For Example, If I give an input date (say january 2nd) , I need the output as Q4 . If I give input as (Jun 5), output should give Q1 .
To get the quarter from a date:Divide the number of the month by 3 . Add 1 to the result. Pass the final value to the Math.
Calculate Fiscal Quarter - CHOOSE Function The CHOOSE function returns a value from a list, based on an index number. Based on the month number, the fiscal quarter number can be returned from a list of numbers. In the example shown below, the fiscal year starts in April, so January is fiscal quarter 4.
public DateTime GetCurrentQuarter(DateTime date)
{
int startingMonthOfQuarter = (((date.Month - 1) / 3) * 3) + 1;
return new DateTime(date.Year, startingMonthOfQuarter, 1);
}
var currentTime = DateTime.Now;
var year = currentTime.Year;
DateTime[] quarterStarts = {new DateTime(year, 1, 1), new DateTime(year, 4, 1), new DateTime(year, 7, 1), new DateTime(year, 10, 1)};
var currentStart = quarterStarts.Where(s => s < currentTime).Last();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With