\"2011-01-30\" How can i convert in c#? I tried with .Tostring(yyyy-MM-dd). It dint work. Thanks in advance.If your input is a strin" />
开发者

how to convert date format in C#

i need to convert a date 开发者_开发知识库from

"01/30/2011" -> "2011-01-30"

How can i convert in c#? I tried with .Tostring(yyyy-MM-dd). It dint work. Thanks in advance.


If your input is a string (rather than a DateTime), that's why your ToString call didn't work. You simply need to parse it first, then format the resulting value:

// I'll be honest: I don't really know if this is the "right" choice or not.
// Maybe someone else can weigh in on that.
IFormatProvider formatProvider = CultureInfo.InvariantCulture;

DateTime date = DateTime.ParseExact("01/30/2011", "MM/dd/yyyy", formatProvider);
string converted = date.ToString("yyyy-MM-dd");


You need to parse your string into a DateTime type and then format it the way you want.

Here is a code snippet:

string d = "01/30/2011";
DateTime dt = DateTime.Parse(s);
string output = string.Format("{0:yyyy-MM-dd}",dt);


This is a simple sample too:

private void button1_Click(object sender, EventArgs e)
{
     textBox1.Text = DateTime.Today.ToString("yyyy-MM-dd");
}


Console.WriteLine("{0:yyyy-MM-dd}", DateTime.Parse("01/30/2011"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜