Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-14
2
Medium Priority
?
697 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:Michael Sterling
[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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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/…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

604 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