我有字符串格式的日期。我想按以下格式转换日期时间。我提到了几个链接,但没有得到准确的输出。
String date = "03-23-16"; //MM-dd-yy
要求:日期格式如“2016年3月23日”
谁能建议我如何转换它?
最佳答案
您可以使用 DateTime.ParseExact
将其转换为 DateTime
,然后使用 格式将其转换回
: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
是月.
关于c# - 如何在 dateTime 中转换字符串日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36349970/