-2

I have a String was not recognized as a valid DateTime error.

Followed one of the examples from here. Which has an example that says:

dateString = "15/06/2008 08:30"; 
  format = "g"; 
  provider = new CultureInfo("fr-FR"); 
  try { 
     result = DateTime.ParseExact(dateString, format, provider); 
     Console.WriteLine("{0} converts to {1}.", dateString, result.ToString()); 
  }    
  catch (FormatException) { 
     Console.WriteLine("{0} is not in the correct format.", dateString); 
  }  

Here's my code:

string convertToString = string.Join("", dateTimeId);

DateTime parsedDateTime = DateTime.ParseExact(convertToString, "g", CultureInfo.InvariantCulture);

I have a string array converted to string because I am reading one csv row which has a datetime value of "3/16/2002 9:20".

I also tried the format for "MM/dd/yyyy HH:mm", but still getting the same error.

Any tip or help is appreciated.

bbusdriver
  • 1,261
  • 2
  • 21
  • 42
  • Why do you use "g"? [That represents the era, like "A.D.", and just that](https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx#gSpecifier). See also: [How do I format a DateTime in a different format?](http://stackoverflow.com/questions/35650681/how-do-i-format-a-datetime-in-a-different-format). – CodeCaster May 12 '16 at 18:15
  • what is the value of `dateTimeId` also why do you feel the need to use the `string.Join() Method` what is it you're trying to accomplish you need to look up the `string.Format` function – MethodMan May 12 '16 at 18:19
  • I'm reading one csv row and save it to a dateTimeId array. dateTimeId = trimedLine.Split(new[] { ',' }).Take(1).ToArray(); – bbusdriver May 12 '16 at 18:20
  • 1
    @pavilion The result of `Split` is a `string[]` so calling `Take` on that will give you a `string`, there is no reason to call `ToArray` (returns a `char[]`) just to recreate the same `string` by passing that into `string.Join` – juharr May 12 '16 at 18:24
  • 1
    @CodeCaster A "g" by itself is the Standard Date Format for a [General date/time pattern (short time)](https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_5) – juharr May 12 '16 at 18:29
  • @juharr did you mean by this, which I have an error? dateTimeId = trimedLine.Split(',').Take(1); – bbusdriver May 12 '16 at 18:33
  • @pavilion No error, just that you don't need to do the `ToArray` and then the `string.Join`. – juharr May 12 '16 at 18:38
  • wow all you guys except for @juharr. It was just the datetime formatting problem, not even related to the string format. You can easily find looking at my post that the problem is related to the datetime format. Look at their problems more carefully and try to give them a solution. Unbelievable. – bbusdriver May 12 '16 at 18:50
  • 1
    @pavilion I think you got down voted because the `string.Join` was really throwing people off. That's why it's best to show a minimal recreation of the problem, which in this case could have just been `DateTime.ParseExact("3/16/2002 9:20", "g", CultureInfo.InvariantCulture)` – juharr May 12 '16 at 19:02
  • @juharr I stand corrected, thanks. – CodeCaster May 12 '16 at 19:10
  • @juharr Thanks for your comment. – bbusdriver May 12 '16 at 19:10

1 Answers1

1

Try this format string instead

"M/dd/yyyy H:mm"

If you specify two M's for the month and two H's for the hour it will expect a leading zero for both.

Or just using DateTime.Parse should work.

Console.WriteLine(DateTime.Parse("3/16/2002 9:20", CultureInfo.InvariantCulture));

The more I look at this the more I think that "g" should have worked. It only seems to work if there are leading zeros for both the month and hour even though it doesn't put leading zeros in if you use it to format a DateTime to a string, at least for InvariantCulture.

juharr
  • 30,127
  • 4
  • 48
  • 88