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();
10/11/2016 23:11:59
. I believe you can retrieve the time portion perfectly, but we never know user wanna have the date11 Oct
or10 Nov
en-US
, and you also gave an example of24/10/2016
, which is not valid in that culture because the US uses month/day/year, and24
is not a valid month. So what you have asked for is unclear and contradictory.