tags:

views:

102

answers:

3

Hi I'm working with c# simple application to display system date time.

textbox.Text = DateTime.Now.ToString("MM/dd/yyyy");

but it is showing result as : 05-12-2010

What is the problem with this code? or do I need to change any where in the regional settings of my machine.

thank you

+6  A: 

the "/" represents the locale datetime seperator. Im guessing that

DateTime.Now.ToString(@"MM\/dd\/yyyy");

will do what you want.

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

hhravn
just missed the '@' there :o)
hhravn
+3  A: 

You may need to specify the culture you want, as the formatting will use the current culture:

textbox.Text = DateTime.Now.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
Oded
A: 
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);
String.Format("{0:MM/dd/yyyy}", dt);          // "03/09/2008"

Source

npinti
Doesn't solve the problem. '/' will still be translated into the local date separator.
hhravn