0

I'm parsing dates and times from an XML using this function:

DateTime.ParseExact(timeEndString, "yyyyMMddHHmm", CultureInfo.InvariantCulture);

Everything is working fine unless the time is 00:00. In thins case I'm getting only date without time. Is there any way to prevent omitting of 0000 time while parsing date time like that?

UPD: Thanks for useful comments. I found out that the problem was in completely different part of the code but not in the parsing part.

Nelrum
  • 113
  • 2
  • 9
  • 1
    What do you mean by "I'm getting only date without time"? A `DateTime` always have a "time" part. – ken2k Apr 23 '13 at 08:52
  • What do you mean? DateTime class always contains time. It will be 00:00:00 in your case. – Tommi Apr 23 '13 at 08:52
  • Put this blog between try...Catch Statement. In the catch put the alternative that is the date without time – noobob Apr 23 '13 at 08:54
  • Right. I didn't realize DateTime always contains time part so the the problem is in completely different part of the code. The problem was that later when I'm converting DateTime in to .ToString(). The part with time had been omitted if it's 00:00:00. – Nelrum Apr 23 '13 at 09:06

2 Answers2

0

just include additonal code logic to check the result length. if it too short, you may assume the zero are stripped, then you just append the zero in.

Kelmen
  • 1,053
  • 1
  • 11
  • 24
0

If you mean that the XML date value can have 2 formats, you can try something like:

try
{
    //...
    DateTime.ParseExact(timeEndString, "yyyyMMddHHmm", CultureInfo.InvariantCulture);
}
catch (Exception)
{
    //...
    DateTime.ParseExact(timeEndString, "yyyyMMdd", CultureInfo.InvariantCulture);
}
noobob
  • 532
  • 4
  • 12