Nearest completed quarter

后端 未结 7 1228
野性不改
野性不改 2021-01-02 00:03

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 = So         


        
7条回答
  •  太阳男子
    2021-01-02 00:27

    Here's a simple function to give you the last day of the current quarter (assuming you're using a standard calendar).

    DateTime LastDayOfQuarter(DateTime today)
    {
        int quarter = (today.Month-1) / 3;
        int lastMonthInQuarter = (quarter +1) * 3;
        int lastDayInMonth =  DateTime.DaysInMonth(today.Year, lastMonthInQuarter);
        return new DateTime(today.Year, lastMonthInQuarter, lastDayInMonth); 
    }
    

    Hope that helps.

提交回复
热议问题