1

I am searching for a solution for truncating the seconds from a TimeSpan object. This is not a formatting request, this is removal.

Initial State var myTimeSpan = new TimeSpan(2, 1, 30, 10);

Desired State 02:01:30:00

Property Change Issue: I have a timer that checks the time every second (desired). The result of having seconds attached to the TimeSpan object is that it fires the PropertyChanged event every second.

Removing the seconds portion will slow the PropertyChanged event firing to 1 minute interval (desired).

Ideas Appreciated - Glenn

user2284452
  • 115
  • 1
  • 11

1 Answers1

2

Just construct a new TimeSpan from your original, with seconds explicitly set to 0:

var newTimeSpan = new TimeSpan(
                        myTimeSpan.Days, myTimeSpan.Hours, 
                        myTimeSpan.Minutes, 0);
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Might be worth looking at this SO question too: http://stackoverflow.com/questions/338658/can-you-round-a-net-timespan-object – Daniel Hollinrake Jun 24 '13 at 16:25
  • @DanielHollinrake That's slightly different, but is similar. The poster here specifically wants truncation, not rounding, though, and wants it to seconds (which makes life far simpler). My option above actually becomes more efficient than many of those, since truncation doesn't require some of those hoops, and is far simpler to follow, too ;) – Reed Copsey Jun 24 '13 at 16:27
  • Thanks Reed - I will give that a go and let you know. Glenn – user2284452 Jun 24 '13 at 17:10
  • @ReedCopsey Thank you clarifying that for me, much appreciated. :-) – Daniel Hollinrake Jun 25 '13 at 08:56