Solved

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

Posted on 2011-09-14
2
676 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:mikesExpertExchange
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Expert Comment

by:disrupt
ID: 36540124
you can do something like so:

ts.Days.ToString().length() == 1 ? "0" + ts.Days.ToString() : ts.Days.ToString()
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 36540125
Pass in "00" to each ToString() call:

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question