C#日期解析精确问题 [英] C# Date Parse Exact Issue
本文介绍了C#日期解析精确问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码产生错误,任何想法为什么?
The following code produces an error, any ideas why?
string dateFormatString = "dd.MM.yyyy HH:mm:ss";
string properDate = DateTime.ParseExact(DateTime.Now.ToString() , dateFormatString , null ).ToString()
错误是:字符串不被识别为有效的日期和时间。
Error is: String is not recognised as a valid date and time.
推荐答案
DateTime.Now.ToString()
使用当前文化格式化日期。您需要指定相同的格式:ParseExact函数预期的 DateTime.Now.ToString(dd.MM.yyyy HH:mm:ss)
DateTime.Now.ToString()
formats the date using the current culture. You need to specify the same format: DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss")
that's expected by the ParseExact function.
这篇关于C#日期解析精确问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文