# MinutesPerDay - Does .NET have anything for it?

Looking at a code review at work for a Win32 C# code I am seeing some lines like this:

int MinutesPerDay = 1440;
int MinutesPerWeek = 10080;
int MinutesPerMonth = 43200;


and so on ...

I am not sure what is wrong with it but still I feel this is not a good code and design? What do you guys think? Could he do it in a better way?

• It should be noted that these values are only valid in timezones which do not participate in Daylight Savings Time. MinutesPerMonth is of course approxiamate, which may not be quite what you want. – Clockwork-Muse Apr 26 '12 at 17:08

There is a class called "TimeSpan", you can use that

TimeSpan interval = TimeSpan.FromDays(1);
double seconds = interval.TotalSeconds;


I don't consider that better from a coding stand point. You should probably look at your code and figure out why you are storing those values, and find a better way.

Since these are constants, I would declare them as such. Also, making the calculation explicit would be more informative.

private const int MinutesPerDay = 24 * 60;
private const int MinutesPerWeek = 7 * MinutesPerDay;
private const int MinutesPerMonth = 30 * MinutesPerDay;

• Because these values are constant by definition, I accept the use of constants here. However, it should be noted that .NET constants are tricky, because the value is copied to the manifest of any other assembly that uses it, requiring a recompile of those assemblies should the value ever change (such as to increase precision of a decimal "constant" like pi, e, Planck's constant, etc). These values, however, are so extremely unlikely to ever change that this isn't a problem. – KeithS Apr 26 '12 at 17:00
• @KeithS: Yes. In scenarios where this could be a problem, you could declare them as static readonly fields. By the way, they are not declared as public here. – Olivier Jacot-Descombes Apr 26 '12 at 17:08
• I would spell out the private in front of const int. StyleCop would ask you to. – Leonid Apr 26 '12 at 20:29
• @Leonid: Yes, you are probably right; however, the context is not clear here, this code could be local to a method. – Olivier Jacot-Descombes Apr 28 '12 at 15:21

The closest thing .NET has is TimeSpan.TicksPerDay

Honestly, using ticks for your time constants is generally a good way to go, because ticks are the root value of DateTime and TimeSpan, and it makes it a little more efficient to compare Time values without having to create new instances of anything by simply comparing the Ticks of either your DateTime or TimeSpan to whatever Ticks constant you need to (be it .NET's or yours).

I have ran into this just last week. I wish TimeSpan had a few more From methods for bigger time periods like weeks months and years, but I made due with these:

    /// <summary>
/// Number of minutes in an hour.
/// </summary>
private static readonly double minutesInHour = TimeSpan.FromHours(1).TotalMinutes;

/// <summary>
/// Number of hours in a day.
/// </summary>
private static readonly double hoursInDay = TimeSpan.FromDays(1).TotalHours;

/// <summary>
/// Number of hours in a week (7 days).
/// </summary>
private static readonly double hoursInWeek = 7.0D * hoursInDay;

/// <summary>
/// Number of hours in a month (30 days).
/// </summary>
private static readonly double hoursInMonth = 30.0D * hoursInDay;


The reason they're doubles are to do multiplication and division calculations later on with user input that could be in a different "base" unit.