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

前端 未结 8 1385
一向
一向 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:24

        public static class DateTimeExtensions
    {
        public static DateTime StartOfQuarter(this DateTime _date)
        {
            var quarter = decimal.ToInt16(Math.Ceiling(_date.Month / 3.0m));
            return new DateTime(_date.Year, quarter, 1);
        }
    }
    

    use

    var quarterStart = DateTime.Today.StartOfQuarter();
    

提交回复
热议问题