C# DateTime.ParseExact 抛出格式异常
我正在使用 MVC3 开发 .NET4 Web 应用程序。
假设我从 XML 源中获取以下日期时间作为字符串。我的应用程序正在读取 xml feed,并且我正在循环遍历它的所有后代。我收到的日期时间开始以以下格式返回(作为字符串);
var myDateTime = "Sun Dec 19 11:45:45 +0000 2010"
我正在使用下面的代码段尝试将上面提到的 DateTime 字符串解析为有效的 DateTime 格式(最好是荷兰语)。
var CorrectDateTime = DateTime.ParseExact(myDateTime , "dd MMM yyyy HH:mm:ss", CultureInfo.InvariantCulture);
当尝试执行此代码时,我遇到了 formatException。有人有什么想法吗?
--更新--
这是我在各种答案之后得到的。但仍然抛出相同的异常。
var correctedDateTime = DateTime.ParseExact(latestTweetTime, "ddd MMM HH:mm:ss K yyyy", CultureInfo.InvariantCulture);
string display = correctedDateTime.ToString("dd MMM yyyy HH:mm:ss");
I'm developing an .NET4 webapplication using MVC3.
Let's say i'm getting the following DateTime as string from an XML-feed. The xml feed is being read by my application and i'm looping through all it's descendants. The DateTime i'm receiving is begin returned in the following format (as string);
var myDateTime = "Sun Dec 19 11:45:45 +0000 2010"
I'm using the piece of code below to try and parse the DateTime string i mentioned above to a valid DateTime format (preferably dutch)
var CorrectDateTime = DateTime.ParseExact(myDateTime , "dd MMM yyyy HH:mm:ss", CultureInfo.InvariantCulture);
When trying to execute this code i'm facing an formatexception. Somebody has got any ideas?
--UPDATE--
This is what i've got after various answers. Still throwing the same exception though.
var correctedDateTime = DateTime.ParseExact(latestTweetTime, "ddd MMM HH:mm:ss K yyyy", CultureInfo.InvariantCulture);
string display = correctedDateTime.ToString("dd MMM yyyy HH:mm:ss");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想阅读此内容:
“Sun Dec 19 11:45:45 +0000 2010”
您需要额外的“d”或“dd”,如下所示:
或
取决于输入是否以零前缀。
您需要考虑输入字符串的每一部分,以下是来自 MSDN 的不同组件的摘要:http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
If you're trying to read this:
"Sun Dec 19 11:45:45 +0000 2010"
You need an additional "d" or "dd" like so:
or
depending on whether the input zero-prefixed.
You need each piece of the input string to be accounted for, here is a summary of the different components from MSDN: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
尝试将您的解析格式更改为:
如果您希望重新格式化
DateTime
,则在解析后调用DateTime.ToString
时指定该格式字符串日期时间
:Try changing your parsing format to this:
If you wish to reformat the
DateTime
then specify that format string when you callDateTime.ToString
on your parsedDateTime
:根据您指定的日期(假设时间为 24 小时),您的输入格式字符串应该是:
所以:
有几件事需要注意,额外的单个 d 来捕获日期,我将使用单个 H 来允许对于“01”和“1”。请参阅 http://msdn.microsoft.com/en -us/library/az4se3k1(v=VS.100).aspx 了解完整格式详细信息。
According to the date you've specified (assuming the time is in 24 hour) your input format string should be:
So:
A couple of things to note, the extra single d to capture the date and I would use a single H to allow for '01' and '1'. See http://msdn.microsoft.com/en-us/library/az4se3k1(v=VS.100).aspx for the full format details.