-1

I'm adding time span with interval size(suppose one minute) in a loop and whenever it gets 23:59 and at this point I'm trying to add one minute, it giving me result 1.00:00:00:00 something like this. How can i get continuous adding intervals when it comes 23:59:00 like

00:00:00
00:01:00
00:02:00

Thanks.

Kiril
  • 39,672
  • 31
  • 167
  • 226
nag
  • 920
  • 6
  • 27
  • 51
  • 6
    "23 hours and 59 minutes" plus "one minute" is "one day". What's the problem? – Richard Deeming Nov 01 '12 at 14:12
  • i don't understand your question. you are adding one minute after another. and finally it get's one day. – Stephan Schinkel Nov 01 '12 at 14:14
  • You want to truncate on just the hour,minute, and seconds part, or at least for displaying. Just do the following `ts.ToString("hh:mm:ss")`. – Alessandro Vermeulen Nov 01 '12 at 14:14
  • @nag, your question is not clear. Are you wanting to have a loop that will add 1 minute onto a timespan until the timespan reaches 23 hours 59 minutes? – Kane Nov 01 '12 at 14:14
  • @Kane Yes after it reaches 23.59 i want 00:00:00 like kind of this without 1 day is this time span helps me on that or any other i need to change? – nag Nov 01 '12 at 14:19
  • What are you using the TimeSpan for? Are you actually using the values, or just using it for display? – Wonko the Sane Nov 01 '12 at 14:24
  • Yeah i'm using these time span values in binding to list. – nag Nov 01 '12 at 14:27
  • @nag - what does that mean? Are you using the actual values in the list, or are you just creating a list of displayed values? – Wonko the Sane Nov 01 '12 at 14:28

5 Answers5

4

Either

  • When displaying your timespan, ignore the days part:
string toDisplay = yourTimeSpan.ToString("hh:mm:ss");
  • After incrementing your timespan, subtract a day if it is a day or longer:
if (yourTimeSpan.Days == 1)
  yourTimeSpan = yourTimeSpan.Subtract(TimeSpan.FromDays(1));

// or Wonko the Sane's improved version
if (yourTimeSpan.Days > 0) yourTimeSpan = new TimeSpan();
Rawling
  • 49,248
  • 7
  • 89
  • 127
1

Whenever your TimeSpan gets over one day, it will format itself in ToString() adding that day to returned string. You could either format ToString() or substract one day after you accumulate it. If you would like your TimeSpan not to go over one day, check this code:

TimeSpan ts = new TimeSpan();
DateTime dt = new DateTime();
TimeSpan day = dt.AddDays(1) - dt;
TimeSpan minute = dt.AddMinutes(1) - dt;

for (int i = 0; i < 20000; i++)
{
     ts = ts.Add(minute);
     if (ts.TotalDays > 1)
     {
         ts = ts.Subtract(day);
     }
}
Nikola Davidovic
  • 8,556
  • 1
  • 27
  • 33
0

I assume you're printing out the value of your TimeSpan just using the standard toString() method? this will yield the TimeSpan in a "human formatted" time representation, e.g. 25 hours will be 1 day and 1 hour.

If you want to get the total number of minutes, for example use the TimeSpan.TotalMinutes proprety

Stealth Rabbi
  • 10,156
  • 22
  • 100
  • 176
0

You want it to loop back to 00:00 after reaching 23:59?

In your loop, or wherever you're trying to achieve this, check if the Days component is bigger than 0, and if so, subtract one day.

Seems too simple an aswer to be right... what do you really look for?

René Wolferink
  • 3,558
  • 2
  • 29
  • 43
0

After your TimeSpan reaches more than a day, you'll get that form of output. If you still want to show it in hour form, try something like

String.format({0}:{1}:{2}, span.Days*24 + span.Hours, span.Minutes, span.Seconds)
Tyler Lee
  • 2,736
  • 13
  • 24