Obsivus
Obsivus

Reputation: 8359

The string was not recognized as a valid DateTime, There is an unknown word starting at index 25

I am grabbing a pubDate node from a xml file, Item.PubDate have the date that I grabbed.

NewsItem.Date is my datetime column in my database table column.

But I cant seem to parse it to datetime.

I get "The string was not recognized as a valid DateTime, There is an unknown word starting at index 25"

Item.Pubdate have this value: "Thu, 9 May 2013 05:04:18 PDT"

When I try:

NewsItem.Date = DateTime.Parse(item.PubDate);

I get that error.

How come with other xml files that pubDates it works?

and they have "Thu, 09 May 2013 09:15:11 GMT"?

Upvotes: 1

Views: 8204

Answers (2)

Habib
Habib

Reputation: 223402

Your string contains PDT (Timezone info), You can't parse with that in your string replace that with empty string and then parse.

NewsItem.Date = DateTime.Parse(item.PubDate.Replace(" PDT",""));

If the string contains GMT then

DateTime.Parse

s contains the Z or GMT time zone designator, and styles includes the RoundtripKind flag. The date and time are interpreted as UTC.

Upvotes: 2

CathalMF
CathalMF

Reputation: 10055

The PDT is not recognised by DateTime.Parse() or Convert.ToDateTime(). But GMT is recognised which is why your second one works.

You need to specify the value that PDT represents.

See answer here. How to convert this 05:41:33 Apr 23, 2012 PDT value to datetime in C#?

Upvotes: 0

Related Questions