rjovic
rjovic

Reputation: 1247

C# DateTime ParseExact exception

I'm having problems with DateTime.ParseExact method which is throwing exceptions that my input string is not in correct format.

Code follows :

class Program
    {
        static void Main(string[] args)
        {
            var rawDate = "Thu, 08 nov 2012 15:19:18 0";
            var _format = "ddd, dd MMM yyyy HH:mm:ss K";
            var date = DateTime.ParseExact(rawDate, _format, CultureInfo.InvariantCulture);
        }
    }

I found a few similar threads here on SO with exact date format and nobody reports any problem there.

I followed this as my guide :

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

What can be cause of exceptions?

Thank you in advance!

Upvotes: 1

Views: 522

Answers (4)

Rik
Rik

Reputation: 29243

Try this:

var _format = "ddd, dd MMM yyyy HH:mm:ss 0";

You will lose the timezone information, though.

Upvotes: 0

Dan
Dan

Reputation: 21

You timezone is wrong in your input string - it needs to be in the format +00:00.

To test your datetime format strings, run them in reverse:

Console.WriteLine(DateTime.Now.ToString(_format));

which gives

Thu, 08 Nov 2012 15:50:58 +00:00

Upvotes: 2

voluminat0
voluminat0

Reputation: 906

I think your 'K' might be a bit off.

The link here might give an explanation: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#KSpecifier

You can leave this blank and drop the 0 - K

Upvotes: 3

Bradley Thomas
Bradley Thomas

Reputation: 4097

Time zone information looks like the most likely suspect to me.

Upvotes: 1

Related Questions