Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Convert date into MM/DD/YY format in C#

Tags:

date

c#

In My Asp.net webpage I need to display today's date into one of the textbox , so in my form load I wrote the following code

textbox1.text = System.DateTime.Today.ToShortDateString(); 

this line is giving me date like 1/7/09 but I want date like 01/07/09 , Is there anyway I can conver this date into mm/dd/yy format in C#?

like image 218
RBS Avatar asked Jan 07 '09 15:01

RBS


People also ask

How do I change date format to MM DD YY?

First, pick the cells that contain dates, then right-click and select Format Cells. Select Custom in the Number Tab, then type 'dd-mmm-yyyy' in the Type text box, then click okay. It will format the dates you specify.

How convert dd mm yyyy to dd Ye yyyy in C#?

code: var creation_date = DateTime. ParseExact(CreationDateTextBox. Text, "DD-MMM-YYYY",null);


2 Answers

 DateTime.Today.ToString("MM/dd/yy") 

Look at the docs for custom date and time format strings for more info.

(Oh, and I hope this app isn't destined for other cultures. That format could really confuse a lot of people... I've never understood the whole month/day/year thing, to be honest. It just seems weird to go "middle/low/high" in terms of scale like that.)

like image 86
Jon Skeet Avatar answered Sep 20 '22 13:09

Jon Skeet


DateTime.Today.ToString("MM/dd/yy")

Look at the docs for custom date and time format strings for more info.

(Oh, and I hope this app isn't destined for other cultures. That format could really confuse a lot of people... I've never understood the whole month/day/year thing, to be honest. It just seems weird to go "middle/low/high" in terms of scale like that.)

Others cultures really are a problem. For example, that code in portugues returns someting like 01-01-01 instead of 01/01/01. I also don't undestand why...

To resolve that problem i do someting like this:

        IFormatProvider yyyymmddFormat = new System.Globalization.CultureInfo(String.Empty, false);         return date.ToString("MM/dd/yy", yyyymmddFormat); 
like image 24
bruno Avatar answered Sep 20 '22 13:09

bruno