-2

I know there are allot of questions regarding this, but I've been trying all day to get this conversion to work and have had no luck when applying the answers to the same question posted here. Every time I try to Parse the string to a DateTime, I get a "String was not recognized as a valid DateTime" exception. If I use Convert.ToDateTime, I can get a Date back from my string, but I need the hh:ss as well.

Here is my simplified code that is ruining my day:

var test = "2015-05-08T05:00Z";

DateTime testTime = new DateTime();
//testTime = Convert.ToDateTime(test);
testTime = DateTime.ParseExact(test, "mm/DD/yyyy HH:ss",
System.Globalization.CultureInfo.InvariantCulture);

Console.WriteLine(testTime);

Why is this string not recognized as a valid DateTime when trying to convert? All help is appreciated

Parker3306
  • 63
  • 3
  • 9
  • possible duplicate of [Converting a String to DateTime in C#](http://stackoverflow.com/questions/919244/converting-a-string-to-datetime-in-c-sharp) – Sam I am says Reinstate Monica May 11 '15 at 20:56
  • 4
    well, if your date string has `-` in it, why are you telling c# to look for `/`? – Marc B May 11 '15 at 20:56
  • 3
    Look at your format in `ParseExact`. Look at your string. Look back at your format once again. Draw an obvious conclusion. – Pierre-Luc Pineault May 11 '15 at 20:57
  • 1
    Read [Jon Skeet's blog post](http://codeblog.jonskeet.uk/2015/05/05/common-mistakes-in-datetime-formatting-and-parsing/). This probably comes under 'broad pattern incompatibilities'. – Charles Mager May 11 '15 at 21:01
  • @CharlesMager Wow! This applies so well to the current situation, I wasn't expecting that, that's awesome. – Pierre-Luc Pineault May 11 '15 at 21:06
  • Ok, I was not aware that the format had to match the string, I thought you told c# what format you wanted it in. That is where I did not understand. Thank you for the helpful, snideless comments. – Parker3306 May 12 '15 at 03:04
  • Maybe in future to avoid spending all day on problems it may pay to read [the documentation](https://msdn.microsoft.com/en-us/library/w2sa9yss%28v=vs.110%29.aspx) - the opening paragraph says 'Converts ... using the specified format... The format of the string representation must match the specified format exactly.' and the remarks are even clearer, including plenty of examples. – Charles Mager May 12 '15 at 08:09
  • @Charles Mager Yeah, I should have read the documentation. I have a habit of just looking for examples. – Parker3306 May 12 '15 at 14:12

4 Answers4

3

Try this...

var test = "2015-05-08T05:00Z";
DateTime testTime = new DateTime();
testTime = DateTime.Parse(test, null, System.Globalization.DateTimeStyles.RoundtripKind);

Console.WriteLine(testTime);
Console.ReadLine();

Or even with DateTime.ParseExact()

var test = "2015-05-08T05:00Z";

DateTime testTime = new DateTime();
testTime = DateTime.ParseExact(test, "yyyy-MM-ddTHH:ssZ", System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);

Console.WriteLine(testTime);
Console.ReadLine();

Results:

enter image description here

Shar1er80
  • 9,001
  • 2
  • 20
  • 29
0

You are doing an exact parse, which means that the parse format string must match exactly with your date literal string. But your parse format string in ParseExact

  1. uses / instead of - in the test literal string.
  2. has a space instead of the T in the test literal string
  3. does not match Z at the end of your test literal string.
  4. Further it is not in yyyy-MM-dd order of your test literal string.

@Shar1er80' s solution is nice and frees you from having to specify a correct parse format string for ParseExact. I'd recommend going with that.

However, if you want to use ParseExact, you need to do this:

testTime = DateTime.ParseExact(test, "yyyy-MM-ddTHH:ssZ",
    System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AdjustToUniversal);

Note that I added a DateTimeStyle of AdjustToUniversal to ensure that your time is interpreted as UTC. The Z in the parse format string is just there to consume a Z. See https://stackoverflow.com/a/833143/49251 for more info on the issue of Z not actually being a part of the format string per se.

Community
  • 1
  • 1
DWright
  • 9,258
  • 4
  • 36
  • 53
0

The format string you are using ("mm/DD/yyyy HH:ss") doesn't match your input in any way.

Have you looked at the DateTime.ParseExact documentation? You could try something like this:

testTime = DateTime.ParseExact(test, "yyyy-MM-ddTHH:ssZ", 
    System.Globalization.CultureInfo.InvariantCulture, 
    System.Globalization.DateTimeStyles.AssumeUniversal);

A couple of notes:

  1. There is no point in setting testTime = new DateTime() if you are going to parse it on the next line. Just drop that line entirely and use var testTime = DateTime.ParseExact(...);
  2. Are you sure that HH:ss is what you want? That seems like a very strange way to write a time. HH:mm or mm:ss would make more sense.
AJ Richardson
  • 6,610
  • 1
  • 49
  • 59
0

You should fix your expected pattern and take the time zone into account.

If your need a DateTime of DateTimeKind.Local:

var date = DateTime.ParseExact("2015-05-08T05:00Z", "yyyy-MM-dd'T'HH:mm'Z'",
   CultureInfo.InvariantCulture);

If your need a DateTime of DateTimeKind.Utc:

var date = DateTime.ParseExact("2015-05-08T05:00Z", "yyyy-MM-dd'T'HH:mm'Z'",
   CultureInfo.InvariantCulture,
    DateTimeStyles.AssumeUniversal 
  | DateTimeStyles.AdjustToUniversal);
JimiLoe
  • 950
  • 2
  • 14
  • 22