Calculate the start-date and name of a quarter from a given date

前端 未结 8 1430
一向
一向 2020-12-23 14:07

How can I find the start-date and name (1, 2, 3, etc.) of a quarter from a given date?

8条回答
  •  醉酒成梦
    2020-12-23 14:32

    int GetQuarterName(DateTime myDate)
    {
        return (int)Math.Ceiling(myDate.Month / 3.0);
    }
    
    DateTime GetQuarterStartingDate(DateTime myDate)
    {
        return new DateTime(myDate.Year,(3*GetQuarterName(myDate))-2,1);
    }
    

    GetQuarterName gets the "next" integer value of the current month number / 3.

    GetQuarterStartingDate uses the output from GetQuarterName to work out the month value, the year part of the original date, and 1 to represent the first day of the month to return.

    (Apologies for making no sense, I have flu. :( )

提交回复
热议问题