Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leading Zero Date Format C#

Tags:

c#

datetime

I have this function...

private string dateConvert(string datDate) {         System.Globalization.CultureInfo cultEnGb = new System.Globalization.CultureInfo("en-GB");         System.Globalization.CultureInfo cultEnUs = new System.Globalization.CultureInfo("en-US");          DateTime dtGb = Convert.ToDateTime(datDate, cultEnGb.DateTimeFormat);         datDate = dtGb.ToString(cultEnUs.DateTimeFormat.ShortDatePattern);          return datDate; } 

But I want it with the leading zero still on lower digits (1-9) so the date is 11-09-2009 (mm-dd-yyyy)...

Now If I wasn't converting it id use string.Format("{0:d}", dateVar) how do I do this in the conversion?

***** Solution *****

Used a slightly modified version of the answer below (i.e. one that would render).

Convert.ToDateTime(datDate).ToString("MM-dd-yyyy"); 
like image 725
Chris McKee Avatar asked Jan 20 '09 12:01

Chris McKee


People also ask

How do you add leading zeros to a number in C?

Step 1: Get the Number N and number of leading zeros P. Step 2: Convert the number to string using the ToString() method and to pad the string use the formatted string argument “0000” for P= 4. val = N. ToString("0000");

Should dates have leading zeros?

The day of the month. Single-digit days will have a leading zero. The abbreviated name of the day of the week.

Do you put a 0 in front of the month?

Senior Member. If the month is written using letters then it would be unusual to add a leading zero to the date. With dates entirely in numbers, leading zeroes for the date and month are common, but not universal.


2 Answers

return dateTimeValue.ToString("MM-dd-yyyy"); 
like image 157
mmx Avatar answered Oct 04 '22 06:10

mmx


Can't you use the string.Format after you've done the conversion?

ie return string.Format("{0:d}", datDate);

like image 38
Glenn Slaven Avatar answered Oct 04 '22 05:10

Glenn Slaven