.NET constant for number of seconds in a day?

人盡茶涼 提交于 2019-12-08 19:32:24

问题


Does .NET have a constant for the number of seconds in a day (86400)?


回答1:


If you want readability you could use:

(new TimeSpan(1,0,0,0)).TotalSeconds

though just using your own const might be clearer :)




回答2:


It's not a constant value

http://en.wikipedia.org/wiki/Leap_second




回答3:


Number of seconds in a regular day is 86400. But the days when DST changes happen may be shorter or longer.

However, writing 24*60*60 is not a bad practice at all, and it is most likely to be in-lined by the compiler, too!




回答4:


so much effort just for not defining a const for 60 x 60 x 24 ;)




回答5:


It isn't a constant, the number of seconds in a day varies depending on the day and the timezone. Thus it isn't something that Microsoft is likely to offer.




回答6:


closest your going to get w/o specifying you own:

System.TimeSpan.TicksPerDay / System.TimeSpan.TicksPerSecond

you could even wrap this as an extension method...

public static Extensions
{
   public static int SecondsPerDay( this System.TimeSpan ts )
   {
      return   System.TimeSpan.TicksPerDay / System.TimeSpan.TicksPerSecond
   }
}



回答7:


double secondsPerDay = TimeSpan.FromDays(1).TotalSeconds;

This was a comment from @ckarras. Adding it as an answer to make it more visible.




回答8:


It is actually available in the .NET framework. You can get to it like this:

using System;
using System.Reflection;

public static class DateTimeHelpers {
  public static int GetSecondsPerDay() {
    object obj = typeof(DateTime).GetField("MillisPerDay", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null);
    return (int)obj / 1000;
  }
}

Please don't use that.



来源:https://stackoverflow.com/questions/2136428/net-constant-for-number-of-seconds-in-a-day

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!