Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net DateTime fiscal Quarter

Tags:

.net

datetime

Does anyone know how to construct a DateTime in .NET to be the first day of the current quarter?

like image 916
PhilBrown Avatar asked Sep 30 '10 18:09

PhilBrown


People also ask

How do I discover the quarter of a given date C#?

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 .

How do you find the current quarter?

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.

Is there a fiscal quarter function in Excel?

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.


2 Answers

public DateTime GetCurrentQuarter(DateTime date)
{
    int startingMonthOfQuarter = (((date.Month - 1) / 3) * 3) + 1;

    return new DateTime(date.Year, startingMonthOfQuarter, 1);
}
like image 54
Yogesh Avatar answered Oct 12 '22 03:10

Yogesh


        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();
like image 39
danijels Avatar answered Oct 12 '22 01:10

danijels