I know this has been asked a lot of times, but none seems to relate to my problem (all answers specifies another datetime format than the one which is giving me issues):
Convert string(dd/MM/yyyy hh:mm) to datetime format
Converting a String to DateTime
Convert dd/MM/yyyy hh:mm:ss.fff from String to DateTime in C#
Specific example:
Input is a string: 24/10/2016 10:20
I call DateTime.TryParse(input, out output)
The output is a DateTime
: {1/1/0001 12:00:00 AM}
Why? This is a perfectly valid input format from what I know...
Things I tried / restrictions:
- Change the input to have a second:
24/10/2016 10:20:00
, it works - Use
TryParseExact
, specifying this format, it works
However, I cannot use both these solutions as the input is user defined, I cannot force the user to stick to a specific input, and want to accept any reasonably formatted date times. The format I specified in the question seems reasonable (it's the default format outputted by Excel).
I can assume the culture is en-US
Any help would be appreciated.
Update: The top answer to the first question throws an exception... I don't know why that's even up-voted.
Update 2:
Since there are a lot of close requests, here's some minimal working code (duplicated from the answer by Mohit Shrivastava):
string dtstr = "24/10/2016 10:20";
DateTime outdt;
DateTime.TryParse(dtstr, out outdt);
Console.WriteLine(outdt);
Console.ReadLine();