-3

I do not know what I'm over looking... Why does this generate an exception "String was not recognized as a valid DateTime."?

string dt = "7/28/‎2018 ‏‎2:48:32 PM";
try
{
  DateTime Test = DateTime.Parse(dt);
}
catch { }

I stepped through my code several times before I commented it out and replaced with the above as a test. the value I am assigning to dt is actually what gets assigned in my program. I was originally using something like:

if (DateTime.TryParse(dt, out DateTime Timestamp))
{ ... }

Not that it matters, but I'm using Visual Studio 2017 and .Net 4.6.2.

Stephen Lee Parker
  • 1,215
  • 9
  • 19
  • 6
    That means this is _not_ a standart date and time format for your `CurrentCulture` settings. Try `ParseExact` or `TryParseExact` methods instead using with `M/dd/yyyy h:mm:ss tt` format and _proper_ culture like `InvariantCulture`. – Soner Gönül Jul 31 '17 at 14:10
  • See below comments. My current culture is set correctly. I wanted to use current culture and the dates and times will be in the current culture format. I just finished re-installing Visual Studio and now it is compiling correctly and generating the expected output. – Stephen Lee Parker Jul 31 '17 at 17:24

3 Answers3

3

DateTime.Parse() will try to guess the format of the given date. If you are sure the dates will always be in a particular format use ParseExact():

string dt = "7/28/‎2018 ‏‎2:48:32 PM";
try
{
    DateTime Test = DateTime.ParseExact(dt, "M/dd/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
}

Note that h(can also use hh) is used instead of HH because it's a 12 hour format and HH represents 24 hour format

The_Outsider
  • 1,875
  • 2
  • 24
  • 42
  • I had not considerend ParseExact() because I do not know what format I will get time in (may or may not have seconds and or am/pm). That said, ParseExact still did not work. And because it did not, I tried on another machine and the original code worked. THERE IS SOMETHING WRONG ON MY MACHINE. +1 because it made me think to try it on another machine. – Stephen Lee Parker Jul 31 '17 at 17:15
1

DateTime.Parse is culture-sensitive. By default, it uses CultureInfo.CurrentCulture to get the proper datetime format. If you need to parse the string using a different culture, you need to use the Parse overload that takes an IFormatProvider argument.

Example, in an invariant culture (useful for persistence):

var dt = DateTime.Parse("07/31/2017 16:15:26", CultureInfo.InvariantCulture);
Console.Write(dt.ToString(CultureInfo.InvariantCulture)); // 07/31/2017 16:15:26
Luaan
  • 62,244
  • 7
  • 97
  • 116
  • thanks for the info. I actually want to use the current culture. In the case where it will be run in Warsaw, Tel Aviv, Shanghai, I need to use the local culture and the input will be in the local culture format. It is working now. I compiled from a different machine without changing the code. Something is wrong with my machine. I'm removing Visual Studio and re-installing (yes, I tried restarting). – Stephen Lee Parker Jul 31 '17 at 17:21
0

Issue was something in Visual Studio 2017. Re-installed and now the original code works as expected.

Stephen Lee Parker
  • 1,215
  • 9
  • 19