简体   繁体   English

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

[英]How to convert string date in dateTime?

I have date in string format. 我有字符串格式的日期。 I want to convert in date time in following format. 我想以以下格式转换日期时间。 I have referred few links but didn't get exact output. 我已经介绍了几个链接,但未获得确切的输出。

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

Requirement: it should be in date format like "March 23, 2016" 要求:日期格式应为“ 2016年3月23日”

Can anybody suggest me how to convert this? 有人可以建议我如何转换吗?

You could convert it to DateTime by using, say, DateTime.ParseExact and then convert it back to string using format "MMMM dd, yyyy" : 你可以将其转换为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

Also, note that mm is minutes format in C# DateTime while MM is months . 另外,请注意, mmC# DateTime 分钟格式,而MMmonths

try this: 尝试这个:

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

parse to string: 解析为字符串:

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

You could convert it to Long Date Format by using ToLOngDateString() 您可以使用ToLOngDateString()将其转换为长日期格式

 string date = "03-05-16";
 date = date.Replace('-', '/');
 DateTime dt = Convert.ToDateTime(date);
 string newDate = dt.ToLongDateString();

it will print :March, 03, 2016. 它将打印:2016年3月3日。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM