.NET CF.NET 1.0Windows 2003.NET 1.1Windows 2000Windows XP.NET 2.0Mobile AppsC# 2.0IntermediateDevVisual StudioWindows.NETC#
How To Round Time by a Specified Number of Minutes?






2.93/5 (11 votes)
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