Question

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

Was it helpful?

Solution

If you want readability you could use:

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

though just using your own const might be clearer :)

OTHER TIPS

It's not a constant value

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

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!

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

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.

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

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

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.

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
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top