C # DateTime format

I have some code that displays the date in a text box as shown below.

textField.Text = DateTime.Now.ToShortDateString();

It is displayed as

03/11/2011

Does anyone know how I can format here to show it as

03/11/11

Thanks in advance

+3
source share
5 answers

Yes. Look at this Date and Time page .

Or: theDate.ToString("dd/MM/yy")

+12
source
DateTime.Now.ToString("dd/MM/yy")
+3
source

:

string strFormat = "dd/MM/yy";
textField.Text = DateTime.Now.ToString(strFormat);

, , , "M" , "" "m". :

  • MMM:
  • MM:
  • ddd: WEEK
  • d: MONTH
  • HH: 24-
  • mm:
  • yyyy:
+3
DateTime.Now.ToString("dd/MM/yy") 

But you need to remember that it is ToShortDateString()sensitive to culture, returning different lines depending on the regional settings of the computer - above this is not so.

You can change the settings on your computer, in Windows 7, you will find the short date format under Region and Languagethe control panel.

+2
source

Here is an alternative if you don't like formatted strings.

var fp = new System.Globalization.CultureInfo("en-GB");
textField.Text = DateTime.Now.ToString(fp.DateTimeFormat.ShortDatePattern);
+2
source

All Articles