如何在dateTime中转换字符串日期?

时间:2016-04-01 07:01:53

标签: c# windows

我有字符串格式的日期。我想以下列格式转换日期时间。我已经提到了几个链接,但没有得到确切的输出。

String date = "03-23-16"; //MM-dd-yy

要求:它应采用日期格式,如“2016年3月23日”

有人可以建议我如何转换吗?

4 个答案:

答案 0 :(得分:2)

您可以使用DateTime将其转换为DateTime.ParseExact,然后使用格式string将其转换回"MMMM dd, yyyy"

String date = "03-23-16"; //MM-dd-yy note that MM here
DateTime dtInstance = DateTime.ParseExact(date, "MM-dd-yy", null); //this is how you convert string to DateTime
string newDate = dtInstance.ToString("MMMM dd, yyyy"); //this is how you convert it back with format as you want

另请注意,mm中的C# DateTime 分钟格式,MM

答案 1 :(得分:1)

试试这个:

string date = "01-08-2008";
DateTime dt = DateTime.ParseExact("24/01/2013", "mm-dd-yy", CultureInfo.InvariantCulture);

解析为字符串:

dt.ToString("MMMM dd, yyyy");

答案 2 :(得分:1)

DateTime.ParseExact("03-23-16", "MM-dd-yy", null).ToString("MMMM dd, yyyy")

答案 3 :(得分:1)

您可以使用 ToLOngDateString()

将其转换为长日期格式
 string date = "03-05-16";
 date = date.Replace('-', '/');
 DateTime dt = Convert.ToDateTime(date);
 string newDate = dt.ToLongDateString();

将打印:2016年3月3日。