Click here to Skip to main content
6,935,055 members and growing! (13,505 online)
Email Password   helpLost your password?
 
General Programming » Date and Time » Algorithms     Intermediate License: The Code Project Open License (CPOL)

Rounding a DateTime object to a defined number of minutes

By Jason Sobell

A simple two line function to round a DateTime up, down, or to a nearest defined number of minutes.
C# (C#1.0, C#2.0, C#3.0), Dev
Posted:23 Mar 2008
Views:11,246
Bookmarked:22 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
6 votes for this article.
Popularity: 3.39 Rating: 4.35 out of 5

1
1 vote, 16.7%
2

3
2 votes, 33.3%
4
3 votes, 50.0%
5

Introduction

Rounding a DateTime up (or down) to the nearest number of minutes seems a simple requirement, but is surprisingly unintuitive due to the relationships and different properties of DateTime and TimeSpan classes.

I've come across some pretty dreadful examples on the web, so thought I'd post a much simpler (and faster) one.

Using the code

This function accepts the number of minutes to be rounded to.

public enum eRoundingDirection { up, down, nearest }

public DateTime RoundDateTime(DateTime dt, int minutes, eRoundingDirection direction)

{
  TimeSpan t;

  switch (direction)
  {
    case eRoundingDirection.up:
      t = (dt.Subtract(DateTime.MinValue)).Add(new TimeSpan(0, minutes, 0)); break;
    case eRoundingDirection.down:
      t = (dt.Subtract(DateTime.MinValue)); break;
    default:
      t = (dt.Subtract(DateTime.MinValue)).Add(new TimeSpan(0, minutes / 2, 0)); break;
  }

  return DateTime.MinValue.Add(new TimeSpan(0, 
         (((int)t.TotalMinutes) / minutes) * minutes, 0));
}

Points of interest

Why use "default:" instead of "nearest"? Simply because it saves defining a default value for the TimeSpan.

Integer math helps keep this function speedy, and avoids those tricky rounding errors in the milliseconds.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Jason Sobell


Member

Occupation: CEO
Company: QuestMetrics
Location: Australia Australia

Other popular Date and Time articles:

Article Top
 
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralSo so PinmvpLuc Pattyn10:30 6 Mar '10  
GeneralExcellent code! PinmemberKebrite23:17 6 Feb '10  
GeneralRe: Excellent code! PinmemberJason Sobell2:19 7 Feb '10  
GeneralVery Interesting Pinmembermerlin9815:28 24 Mar '08  
Very interesting solution. Thanks for sharing


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Rhabot - World of Warcraft Bot Uber RPGE - Free Private World of Warcraft Server Make long URLs short with NeatURL.net ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 23 Mar 2008
Editor: Smitha Vijayan
Copyright 2008 by Jason Sobell
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project