Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-04-04
7
Medium Priority
?
2,355 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
[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
  • 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 2000 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
Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

 
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 86

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

705 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