6

What's the preferred approach to compare a complete DateTime instance with an hour, minute, and second which represents an actual time of day, with the ability to operate over those triplets (eg add hours, minutes seconds..)?


My current approach is something like

DateTime startHour = new DateTime(1900,1,1,12,25,43);
DateTime endHour = new DateTime(1900,1,1,13,45,32);

// I need to, say, know if a complete DateTime instance 
// is later than startHour plus 15 minutes

DateTime now = DateTime.Now();

startHour = startHour.addMinutes(15);

if (now.CompareTo(new DateTime(now.Year, now.Month, now.Day, startHour.Hour, 
                    startHour.Minute, startHour.Second)) > 0) 
{
    //I can do something now
}

This is very cumbersome and even failure prone. TimeSpans are not a solution as far as I can see, because they represent spans and aren't bound by the 24 hours limit (a TimeSpan of 56 hours 34 minutes is valid.)

What's the preferred approach for this type of calculations?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
  • What problem are you trying to solve? Is it merely that TimeSpan can exceed 24 hours? Because you can easily check for that. – Robert Harvey Sep 10 '09 at 23:09

4 Answers4

11

It's not at all clear what you mean by "is greater than startHour"... but taking

TimeSpan startHour = new TimeSpan(12, 25, 43);
if (endHour.TimeOfDay > startHour)
{
    ...
}

... works pretty simply.

By all means add argument checking to make sure that you don't specify a value for startHour which is < 0 or > 23 hours, but that's all pretty easy.

.NET's date and time API is quite primitive (even in 3.5) compared with, say, Joda Time - but in this particular case I think it's not too bad.

Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    This is the first time I've heard someone refer to the .NET date/time API as "primitive" in a negative tone, and I couldn't disagree more. :) – Sam Harwell Sep 10 '09 at 23:20
  • @280Z284: Until 3.5 you couldn't even use non-local time zones! Compared with Joda, .NET really is quite primitive. There's no type representing an instant with an associated time zone - just a UTC offset. Blech. Would consider porting Joda if I had more time. – Jon Skeet Sep 10 '09 at 23:26
  • Yeah, Joda rocks. Especially when compared to native Java Datetime. – Vinko Vrsalovic Sep 10 '09 at 23:49
  • Are there any .NET libraries out there that can help? – Erik Forbes Sep 10 '09 at 23:50
  • Or, perhaps, a .NET conversion for Joda Time? – Erik Forbes Sep 10 '09 at 23:54
  • @Erik: Not as far as I know. I keep wondering about a .NET port of Joda Time, but I don't have enough expertise or time... – Jon Skeet Sep 11 '09 at 05:24
2

A little hint - .NET supports arithmetic operations on DateTime objects, and returns a TimeSpan object. Thus, you can do the following:

DateTime fromDate = ....
DateTime toDate = ....
TimeSpan diff = toDate - fromDate;

and you can expand this to:

DateTime fromDate = DateTime.Now;
DateTime toDate = DateTime.Now.addMinutes(x);

if ((toDate - fromDate).TotalMinutes > 15) {
    ...
}
Fritz H
  • 3,539
  • 2
  • 26
  • 37
1

You should use TimeSpan for startHour and endHour. When comparing with now, you should "convert" them to a full DateTime or get the Time with DateTime.TimeOfDay as mentioned by Jon Skeet.


TimeSpan startHour = new TimeSpan(12, 25, 43);
DateTime now = DateTime.Now;

if (now.CompareTo(DateTime.Today.Add(startHour)) > 0) {
    //...
}

or


TimeSpan startHour = new TimeSpan(12, 25, 43);
DateTime now = DateTime.Now;

if (now.TimeOfDay.CompareTo(startHour) > 0) {
    //...
}
Alfred Myers
  • 6,384
  • 1
  • 40
  • 68
  • Why do it that way instead of taking the current time of day? And why use CompareTo instead of the handily-overloaded "greater than" operator? – Jon Skeet Sep 10 '09 at 23:12
0

So you're only interested in the time component of the date.

if(DateTime.Now.TimeOfDay > startHour.TimeOfDay)
{
  // do stuff
}

What's wrong with doing this?

OJ.
  • 28,944
  • 5
  • 56
  • 71
  • It will work, but there's no need for the Date part of startHour and endHour. – Alfred Myers Sep 10 '09 at 23:18
  • Yes, but that's if you assume that the snippet that was provided is the only part of the code that uses startHour. But what if startHour's date value is used elsewhere? – OJ. Sep 14 '09 at 04:42