0

I have a datetime string and am trying to parse that into a DateTime object, but currently it is not working. My two DateTime objects never have their values set. I'm sure it's something very simple I'm just not seeing. Does anyone see anything obviously wrong with this code?

string Issued = "Tue 25 Jul 2017 16:47:38";
string Expires = "Tue 25 Jul 2017 18:47:38";

string format = "ddd dd MMM yyyy HH:mm:ss";
DateTime dIssued;
DateTime dExpires;
DateTime.TryParseExact(Issued, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dIssued);
DateTime.TryParseExact(Expires, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dExpires);
mituw16
  • 5,126
  • 3
  • 23
  • 48
  • 3
    How are you determining the values of the `DateTime`s? I'm seeing it work: https://dotnetfiddle.net/s9JVz7 – Heretic Monkey Jul 25 '17 at 17:12
  • 1
    Where are your `Issued` and `Expired` strings coming from? Could there be non-printable characters in there? Something that looks like, but isn't, a space? – krillgar Jul 25 '17 at 17:14
  • 1
    I ran the program and seems like it's working fine? – Sach Jul 25 '17 at 17:15
  • @MikeMcCaughan Thanks for verifying I'm not crazy! :) Something else must be wrong. – mituw16 Jul 25 '17 at 17:16
  • @krillgar you might be onto something, these values are coming from an api call, and it's possible they aren't true space characters – mituw16 Jul 25 '17 at 17:16
  • I checked what @krillgar suggested, and they are true spaces. Okay so this is weird.. it's obviously working on dotnetfiddle, but inside of my code, the same exact code I pasted, is not working. After doing the tryparse exact. Both datetime objects still have the value of `1/1/0001 12:00:00 AM` .. Anyone see anything else? This is maddening. – mituw16 Jul 25 '17 at 17:21
  • I agree with @krillgar. There might be some special characters in there. – JuanR Jul 25 '17 at 17:24
  • Try this in the string you are getting from the api call and let us know if you get a match: https://stackoverflow.com/questions/4503542/check-for-special-characters-in-a-string – JuanR Jul 25 '17 at 17:35

1 Answers1

1

I ran your code and it seemed to work fine. So I'm assuming you're hard coding those strings for exemplary sake. Consider putting a break statement at the point in your code where those two values are filled and inspecting them to see their content. Any excess whitespace or chars will throw an error.

Try .trim(); on your string too. This essentially removes any white space at the beginning and end of the string so it would transform my string like so:

" cat " -> "cat"

Capn Jack
  • 1,201
  • 11
  • 28
  • Ahhhh.. That's exactly what it was. There was one extra space character that I didn't notice in the debugger. Thanks! – mituw16 Jul 25 '17 at 17:24
  • 1
    @mituw16 debugging is so so SO helpful. Without it it's ridiculously difficult to program properly. – Capn Jack Jul 25 '17 at 17:30
  • 1
    I agree completely.. I've been staring at the values in the debugger for the last 45 minutes, and completely missed the extra space in the string. Needed that second pair of eyes :) – mituw16 Jul 25 '17 at 17:32
  • @mituw16 yeah not a problem. From now you'll probably be in the habit (like I am) of being scrutinous when analyzing strings that are throwing errors. – Capn Jack Jul 25 '17 at 17:36