Link to home
Start Free TrialLog in
Avatar of Michael Sterling
Michael SterlingFlag for United States of America

asked on

How do I add leadding zeros for my timespan date parts when I need them?

I have a TimeSpan object that I'm breaking into its individual parts  (days, hours, minutes, seconds), as strings, using the ToString() method. How do I ensure that for each part that is a single digit, that I add a leading 0? for example, this: 1:6:45:9 would become this: 01:06:45:09
.
.
.
TimeSpan ts = currentDateTime - signInTime;


return ts.Days.ToString() + ":" + ts.Hours.ToString() + ":" + ts.Minutes.ToString() + ":" + ts.Seconds.ToString();
.
.
.

Open in new window

Avatar of disrupt
disrupt
Flag of United States of America image

you can do something like so:

ts.Days.ToString().length() == 1 ? "0" + ts.Days.ToString() : ts.Days.ToString()
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial