Rounding a datetime to nearest hour.

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
Any suggestions on how to do this, I am trying to get the number of minutes associated with the time at the moment and then use the Subtract method, don't think that this is the best approach and is going to cause a lot of problems.

Mike55.
 
DateTime constructor

How about this approach:

C#:
DateTime roundTime = new DateTime(
                         rawTime.Year,
                         rawTime.Month,
                         rawTime.Day,
                         (rawTime.Minute < 30) ? rawTime.Hour : rawTime.Hour + 1,
                         0,
                         0
                     );

Good luck :cool:
 
Back
Top