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
2010-01-25 23:23:07
Not a constant, but leaves me out of the second-counting business.
lance
2010-01-25 23:33:18
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
2010-01-26 18:47:57
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
2010-01-25 23:42:23
Leap seconds are however not used in the standard .NET DateTime representation. So irrelevant for all except specialized applications.
Joe
2010-01-26 06:01:48
+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
2010-01-25 23:25:27
+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
2010-01-25 23:26:16
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
2010-01-26 01:47:29
+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
2010-01-25 23:49:48
+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
2010-01-26 01:03:41
Well, it shouldn't be. It smoothly handles the .NET update required after the Earth's rotation slows down enough.
Hans Passant
2010-01-26 02:01:51