Click here to Skip to main content
Licence CPOL
First Posted 23 Jan 2007
Views 31,439
Bookmarked 18 times

How To Round Time by a Specified Number of Minutes?

By | 23 Jan 2007 | Article
This is an algorithm formula to calculate round time...

Introduction

This is a very simple code to show how we can calculate the round time in C# .NET...

Using the Code

You can write the below code in your Windows application form_load method and test it...

// Sample time: 01:07:00
TimeSpan t = new TimeSpan(1, 7, 0);

// The round number, here is a quarter...
int Round = 15;

// Count of round number in this total minutes...
double CountRound = (t.TotalMinutes / Round);

// The main formula to calculate round time...
int Min = (int)Math.Truncate(CountRound + 0.5) * Round;

// Now show the result...
TimeSpan tRes = new TimeSpan(0, Min, 0);
MessageBox.Show(tRes.ToString());

You can change the Round number from 15 to every number you want round on, for example 5 or 10 or ...

Good luck!

History

  • 23rd January, 2007: Initial post

License

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

About the Author

Alireza Naghizadeh

Software Developer (Senior)

Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member

Alireza Naghizadeh

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalfor include seconds in round time PinmemberMember 44056005:12 10 Apr '10  
QuestionRound to nearest value or to a future time value? Pinmemberabentov3:46 1 Mar '07  
GeneralDateTruncate PinmemberPIEBALDconsult9:53 7 Feb '07  
GeneralWorks only in .net 2 Pinmemberckl_889:33 7 Feb '07  
GeneralRe: Works only in .net 2 PinmemberAlireza Naghizadeh18:41 7 Feb '07  
That's right
Thank you for your attention...
GeneralRe: Works only in .net 2 Pinmemberckl_886:33 8 Feb '07  
GeneralGreat article Pinmemberaprenot5:58 24 Jan '07  
GeneralRe: Great article PinmemberAlireza Naghizadeh23:25 26 Jan '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 24 Jan 2007
Article Copyright 2007 by Alireza Naghizadeh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid