How to fill a string with 0 on the left in C#

Posted on 2007-04-04
Last Modified: 2013-12-16
Hi Guys,
I have a string called FullFileName and it is returnig "F-4420079251". I need that returns
"F-04042007092501". How can I add 0 on the left when the day only has one character?

DateTime FileDate    = objFileInfo[i].LastWriteTime;
string FullFileName  = "F-" + FileDate.Month + FileDate.Day + FileDate.Year +
                                       FileDate.Hour + FileDate.Minute + FileDate.Second

Question by:Carolinat
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
  • 4
  • 2
LVL 23

Expert Comment

by:Jens Fiederer
ID: 18851614
You can use ToString with a format option to specify the zero-fill
LVL 23

Accepted Solution

Jens Fiederer earned 500 total points
ID: 18851622
For example, FileDate.Day.ToString("00") will give you from "00" to "31"
LVL 23

Expert Comment

by:Jens Fiederer
ID: 18851629
You probably want to do that on the other components (month, hour, minutes,...) as well.
Independent Software Vendors: 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!

LVL 22

Expert Comment

ID: 18851633
string FullFileName  = "F-" + FileDate.Month.ToString("00") + FileDate.Day.ToString("00") + FileDate.Year. + FileDate.Hour + FileDate.Minute + FileDate.Second
LVL 22

Expert Comment

ID: 18851635

d'oh!  too slow....
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18851929
You can do it with only ONE ToString() though:

DateTime FileDate    = objFileInfo[i].LastWriteTime;
string FullFileName  = "F-" + FileDate.ToString("MMddyyyyhhmmss");

LVL 23

Expert Comment

by:Jens Fiederer
ID: 18852055
Good point, but you probably want to use HH rather than hh, to distinguish the AM from the PM.

The original attempt displayed hours from 0-23.

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

735 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