views:

263

answers:

7

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

+3  A: 

If you want readability you could use:

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

though just using your own const might be clearer :)

GaussZ
Not a constant, but leaves me out of the second-counting business.
lance
This simple answer made me rethink the approach I was taking. It's not quite the code I used to ultimately solve my problem, but I'm all for choosing an answer based on the fishing it taught rather than the fish it provided. Thank you.
lance
+19  A: 

It's not a constant value

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

Jason
That's unexpected :)
dr. evil
Leap seconds are not the only problem, days where daylight saving time changes occur have an hour less or more (counting from 00:00-24:00) in the according timezones.
GaussZ
Fortunately, in about 363/365 cases, 86400 is good enough. ;)
Greg D
@GaussZ: That is why you should always store times as UTC.
Aaronaught
Leap seconds are however not used in the standard .NET DateTime representation. So irrelevant for all except specialized applications.
Joe
+2  A: 

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
   }
}
Muad'Dib
+3  A: 

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.

Jonathan Allen
you won't tell me that a day in arizona is not the same 24 hours long as it is in e.g. singapour?
Kai
+4  A: 

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!

naivists
+2  A: 

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.

Hans Passant
Doesn't look like a constant to me... :)
stakx
Well, it shouldn't be. It smoothly handles the .NET update required after the Earth's rotation slows down enough.
Hans Passant
+1  A: 

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

Kai