如何在C#中转换日期格式

Bal*_*ala 0 c#

我需要转换日期

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

我怎么能用c#转换?我试过.Tostring(yyyy-MM-dd).它起作用了.提前致谢.

Dan*_*Tao 5

如果您的输入是string(而不是a DateTime),那就是您的ToString通话无效的原因.您只需要先解析它,然后格式化结果值:

// 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");
Run Code Online (Sandbox Code Playgroud)