Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add leading zeroes to String.Format parameters?

Tags:

c#

I have the following code:

string textTransDate = String.Format("{0}-{1}-{2} {3}:{4}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute);

I want to add leading zeroes to the month and day parameters if they are less than 10, how would I accomplish this?

like image 795
Corne Beukes Avatar asked Dec 07 '22 16:12

Corne Beukes


1 Answers

string textTransDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm");

More info here:

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

like image 193
Jakub Konecki Avatar answered Jan 07 '23 19:01

Jakub Konecki