问题
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