Solved

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

Posted on 2007-04-04
7
2,317 Views
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

Thanks,
Carolina
0
Comment
Question by:Carolinat
  • 4
  • 2
7 Comments
 
LVL 23

Expert Comment

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

Accepted Solution

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

Expert Comment

by:Jens Fiederer
ID: 18851629
You probably want to do that on the other components (month, hour, minutes,...) as well.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 22

Expert Comment

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

Expert Comment

by:_TAD_
ID: 18851635

d'oh!  too slow....
0
 
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");

See:
http://msdn2.microsoft.com/en-us/library/8kb3ddd4(vs.80).aspx
0
 
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Library to convert HTML to PDF. 8 46
Image(7) 1 37
Problem to open Excel file 15 44
COnsume rest client 6 15
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now