0

I am trying to retrieve some string from xml and convert it into equivalent date but it's not working out.Following is the code I am using:

var Pubdate = DateTime.Parse((sourcei.Element("pubDate").Value).Replace("\t","").Replace("\n",""));

This is the actual tag which I am trying to parse:

<pubDate>

                        Mon, 09 Sep 2013 13:47:57 EDT 

                </pubDate>

(Yes,there are spaces in original tag).

So,what's going wrong?

Rajat Saxena
  • 3,834
  • 5
  • 45
  • 63
  • 1
    Show the string without any replacements – Developer Sep 09 '13 at 18:33
  • Mon, 09 Sep 2013 13:47:57 EDT – Rajat Saxena Sep 09 '13 at 18:35
  • And there is a large sequence on "/n" and "/t" before and after that date string. – Rajat Saxena Sep 09 '13 at 18:36
  • 1
    `it's not working` is a horrendous problem description. Be more specific. Compile error? Runtime error? Unexpected output? What did you expect the output to be? What did it turn out to be instead? – tnw Sep 09 '13 at 18:36
  • Also, don't add extra details of your question as a comment. EDIT your original question and clarify. – tnw Sep 09 '13 at 18:38
  • @tnw: Well,I already stated in my question that I'm getting a runtime error "FormatException" while parsing the date obtained from xml tag. – Rajat Saxena Sep 09 '13 at 18:40
  • Derek's answer is the way to go, you can also refer to [this question & answer](http://stackoverflow.com/a/5657081/1860561) if you can't strip away the EDT – gitsitgo Sep 09 '13 at 20:30

3 Answers3

1

The EDT is causing it to fail.

see: Parse DateTime with time zone of form PST/CEST/UTC/etc

I don't get an error if I remove "EDT" from the string.

You can test with:

var pubDate = DateTime.Parse( "Mon, 09 Sep 2013 13:47:57 EDT" );

and

var pubDate = DateTime.Parse( "Mon, 09 Sep 2013 13:47:57" );
Community
  • 1
  • 1
Derek
  • 7,615
  • 5
  • 33
  • 58
0

You should not parse directly using DateTime.Parse() method ,because if your system date cannot map with your date string format,then it may resulting unexpected error.So you have to try this way.

DateTime dateValue;
DateTime dateValue1;
CultureInfo culture = CultureInfo.CurrentCulture;
DateTimeStyles styles = DateTimeStyles.None;
DateTime.TryParse("" + sourcei.Element("pubDate").Value.Trim(), new CultureInfo("en-US"), styles, out dateValue);
dateValue1 = DateTime.Parse(""+dateValue.ToShortDateString());
StringBuilder dateValue2 = new StringBuilder("" + dateValue1.ToString("MM/dd/yyyy"));
dateValue2 = dateValue2.Replace("-", "/");

Note:Here i am taking the example of DateString format is "MM/dd/yyyy".And so you need to change this format as per your requirement.

-1

Try using Parse exact.

DateTime.ParseExact(STRING, "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture);

"dd.MM.yyyy" - what format your string is

Developer
  • 4,158
  • 5
  • 34
  • 66
  • -1, OP's string is definitely not in that format and we also still don't know what exactly is going wrong. – tnw Sep 09 '13 at 18:39