C# - How to calculate the current day-of-year?

前端 未结 5 1556
栀梦
栀梦 2021-01-12 00:54

Today is 5.27.2010 - this means it is day 147 of this year.

How do I calculate that today is 147 based on the current date?

5条回答
  •  遥遥无期
    2021-01-12 01:16

    There's a DateTime property named just that: DayOfYear

    Console.WriteLine(DateTime.Now.DayOfYear);
    

    Or for any date:

    var d = new DateTime(2010, 5, 30);
    Console.WriteLine(d.DayOfYear);
    

提交回复
热议问题