Nearest completed quarter

后端 未结 7 1196
野性不改
野性不改 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条回答
  •  Happy的楠姐
    2021-01-02 00:14

    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.

    public DateTime LastDayOfLastQuarter(DateTime date)
    {
        int result = (int)(date.Month/3)
    
        switch (result)
        {
            // January - March
            case 0:
                return new DateTime(date.Year - 1, 12, 31);
            // April - June
            case 1:
                return new DateTime(date.Year, 3, 31);
            // July - September
            case 2:
                return new DateTime(date.Year, 6, 30);
            // October - December
            case 3:
                return new DateTime(date.Year, 9, 30);
        }
    }
    

提交回复
热议问题