Solved

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

Posted on 2011-09-14
2
682 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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

718 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