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

前端 未结 5 1544
栀梦
栀梦 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:10

    C#'s DateTime class has a method called DayOfYear() that you could use.

    0 讨论(0)
  • 2021-01-12 01:12
    DateTime dt = new DateTime(2001, 12, 14);
                dynamic dayofyear = dt.DayOfYear;
                dynamic datofweek = dt.DayOfWeek;
    
    0 讨论(0)
  • 2021-01-12 01:14

    Has anyone mentioned the DateTime.DayOfYear property?

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-01-12 01:22

    Actually, it's pretty easy:

    int dayOfYear = DateTime.Today.DayOfYear;
    
    0 讨论(0)
提交回复
热议问题