C# DateTime.ParseExact 抛出格式异常

发布于 2024-10-08 20:07:53 字数 724 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱殇璃 2024-10-15 20:07:53

如果您想阅读此内容:
“Sun Dec 19 11:45:45 +0000 2010”

您需要额外的“d”或“dd”,如下所示:

"ddd MMM d HH:mm:ss K yyyy"

"ddd MMM dd HH:mm:ss K yyyy"

取决于输入是否以零前缀。

您需要考虑输入字符串的每一部分,以下是来自 MSDN 的不同组件的摘要:http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

ddd = Three letter Day of week
MMM = Three letter month
dd = Two digit day of month 01-31  (use "d" for 1-31)
HH = Hours using 24-hour clock. 00-24  (use "H" for 0-24)
mm = Minutes. 00-59
ss = Seconds. 00-59
K = Time zone information
yyyy = 4-digit year

If you're trying to read this:
"Sun Dec 19 11:45:45 +0000 2010"

You need an additional "d" or "dd" like so:

"ddd MMM d HH:mm:ss K yyyy"

or

"ddd MMM dd HH:mm:ss K yyyy"

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

ddd = Three letter Day of week
MMM = Three letter month
dd = Two digit day of month 01-31  (use "d" for 1-31)
HH = Hours using 24-hour clock. 00-24  (use "H" for 0-24)
mm = Minutes. 00-59
ss = Seconds. 00-59
K = Time zone information
yyyy = 4-digit year
我的痛♀有谁懂 2024-10-15 20:07:53

尝试将您的解析格式更改为:

"ddd MMM HH:mm:ss K yyyy"

如果您希望重新格式化 DateTime,则在解析后调用 DateTime.ToString 时指定该格式字符串日期时间

string display = CorrectedDateTime.ToString("dd MMM yyyy HH:mm:ss");

Try changing your parsing format to this:

"ddd MMM HH:mm:ss K yyyy"

If you wish to reformat the DateTimethen specify that format string when you call DateTime.ToString on your parsed DateTime:

string display = CorrectedDateTime.ToString("dd MMM yyyy HH:mm:ss");
弥枳 2024-10-15 20:07:53

根据您指定的日期(假设时间为 24 小时),您的输入格式字符串应该是:

ddd MMM d H:mm:ss K yyyy. 
Sun Dec 19 11:45:45 +0000 2010

所以:

var correctedDateTime = DateTime.ParseExact(myDateTime, "ddd MMM d H:mm:ss K yyyy", CultureInfo.InvariantCulture);
string display = correctedDateTime.ToString("dd MMM yyyy HH:mm:ss");

有几件事需要注意,额外的单个 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:

ddd MMM d H:mm:ss K yyyy. 
Sun Dec 19 11:45:45 +0000 2010

So:

var correctedDateTime = DateTime.ParseExact(myDateTime, "ddd MMM d H:mm:ss K yyyy", CultureInfo.InvariantCulture);
string display = correctedDateTime.ToString("dd MMM yyyy HH:mm:ss");

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文