Reputation: 77945
I am trying to parse Date/Time formats in HTTP/1.1 headers as specified in RFC2616
How do I parse an ANSI C timestamp in C#?
The closest I get is:
string dateFormat = "ddd MMM d HH':'mm':'ss yyyy";
DateTime time = DateTime.ParseExact("Mon Jan 2 15:04:05 2006", dateFormat, CultureInfo.InvariantCulture);
The problem lies in "d" which does not accept a leading space in case it is a single digit date. And "dd" requires a leading 0 instead.
Is there any easy way around, or maybe a library that already handles the three allowed date/time formats in HTTP/1.1?
Upvotes: 1
Views: 832
Reputation: 98740
How about using DateTime.TryParseExact
overload that takes string[]
as a format parameter with AllowInnerWhite
style?
string s = "Sun, 06 Nov 1994 08:49:37 GMT";
DateTime dt;
var formats = new[]
{
"ddd, dd MMM yyyy HH:mm:ss 'GMT'",
"dddd, dd-MMM-yy HH:mm:ss 'GMT'",
"ddd MMM d HH:mm:ss yyyy"
};
if(DateTime.TryParseExact(s, formats, CultureInfo.InvariantCulture,
DateTimeStyles.AllowInnerWhite, out dt))
{
//
}
Upvotes: 2