Nearest completed quarter

后端 未结 7 1202
野性不改
野性不改 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:17

    Try this:
    The AddMonths(-(d.Month-1) % 3)) moves the datevalue to the equivilent day of the first month in the quarter, and then the AddDays (-day) moves back to the last day of the preceding month.

      DateTime d = Datetime.Now;
      DateTime lastDayOfLastQuarter = d.AddMonths(-((d.Month-1)%3)).AddDays(-d.Day);
    

提交回复
热议问题