Convert DateTime format

net 4 and C #.

I have a DateTimeValue text box

3/1/2011 12:00:00 AM

I need to convert it to a string of this format:

Format="yyyy-MM-dd"

Any idea how to do this? PS: I can delete time information

thank you for your time

+5
source share
7 answers

Use DateTime.ParseExact like this:

DateTime.ParseExact("3/1/2011 12:00:00 AM", "G", 
           CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd");

Be sure to indicate the culture, because the format you use is ambiguous.

+7
source

Is this what you are asking for?

DateTime.Parse("3/1/2011 12:00:00 AM").ToString("yyyy-MM-dd")
+2
source

: System.Globalization, CultureInfo.

: using System.Globalization; // for CultureInfo

+1

Convert.ToDateTime(Text1.Text).ToString( "--" )

0

. . , , . () DateTime.Parse.

0

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

, :

DateTime date = new DateTime(2011, 3, 1);
date.ToString("yyyy-MM-dd")
0

string dt = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();

0

All Articles