[英]C# DateTime.ParseExact throwing format exception
I'm developing an .NET4 webapplication using MVC3. 我正在使用MVC3开发.NET4 Web应用程序。
Let's say i'm getting the following DateTime as string from an XML-feed. 假设我从XML提要中获取以下DateTime作为字符串。 The xml feed is being read by my application and i'm looping through all it's descendants. 我的应用程序正在读取xml提要,而我正在遍历它的所有后代。 The DateTime i'm receiving is begin returned in the following format (as string); 我正在接收的DateTime开始以以下格式(作为字符串)返回;
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) 我正在使用下面的代码片段来尝试将上面提到的DateTime字符串解析为有效的DateTime格式(最好是荷兰语)
var CorrectDateTime = DateTime.ParseExact(myDateTime , "dd MMM yyyy HH:mm:ss", CultureInfo.InvariantCulture);
When trying to execute this code i'm facing an formatexception. 当尝试执行此代码时,我遇到了formatexception。 Somebody has got any ideas? 有人有什么主意吗?
--UPDATE-- --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");
Try changing your parsing format to this: 尝试将解析格式更改为此:
"ddd MMM HH:mm:ss K yyyy"
If you wish to reformat the DateTime
then specify that format string when you call DateTime.ToString
on your parsed DateTime
: 如果要重新格式化 DateTime
则在解析的DateTime
上调用DateTime.ToString
时,请指定该格式字符串:
string display = CorrectedDateTime.ToString("dd MMM yyyy HH:mm:ss");
If you're trying to read this: "Sun Dec 19 11:45:45 +0000 2010" 如果您要阅读以下内容:“ Sun Dec 19 11:45:45 +0000 2010”
You need an additional "d" or "dd" like so: 您需要这样的附加“ d”或“ dd”:
"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 您需要考虑每个输入字符串,这是MSDN中不同组件的摘要: http : //msdn.microsoft.com/zh-cn/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
According to the date you've specified (assuming the time is in 24 hour) your input format string should be: 根据您指定的日期(假设时间为24小时),您的输入格式字符串应为:
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'. 有两点需要注意,额外的d表示日期,我将使用单个H表示'01'和'1'。 See http://msdn.microsoft.com/en-us/library/az4se3k1(v=VS.100).aspx for the full format details. 有关完整格式的详细信息,请参见http://msdn.microsoft.com/zh-cn/library/az4se3k1(v=VS.100).aspx 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.