65.9K
CodeProject is changing. Read more.
Home

How To Round Time by a Specified Number of Minutes?

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.93/5 (11 votes)

Jan 24, 2007

CPOL
viewsIcon

63571

downloadIcon

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...

// 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