Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile

How To Round Time by a Specified Number of Minutes?

2.93/5 (11 votes)
23 Jan 2007CPOL 1   1  
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...

C#
// 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)