-3

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();

Minimal code

18
  • 2
    Could you please also show the piece of code you are running.
    – Mohit S
    Commented Oct 25, 2016 at 1:22
  • 3
    Allowing the user to define the format is asking for trouble. That's why it is better to either have separate entry fields or drop-downs for day, month & year or to restrict the format.
    – Dijkgraaf
    Commented Oct 25, 2016 at 1:24
  • 1
    @Dijkgraaf This is getting parsed from a CSV file. I'm trying to accommodate for all reasonably formatted date times, if it's something crazy, then I will happily drop the record.
    – l3utterfly
    Commented Oct 25, 2016 at 1:25
  • 4
    I don't think you will have a perfect solution, for example, when user input 10/11/2016 23:11:59. I believe you can retrieve the time portion perfectly, but we never know user wanna have the date 11 Oct or 10 Nov
    – Prisoner
    Commented Oct 25, 2016 at 1:27
  • 3
    Alex is right, you cannot tell if the input is month/day/year or day/month/year without having the culture specified. BUT - you stated that you could assume en-US, and you also gave an example of 24/10/2016, which is not valid in that culture because the US uses month/day/year, and 24 is not a valid month. So what you have asked for is unclear and contradictory. Commented Oct 25, 2016 at 1:49

2 Answers 2

2

Your output is 1/1/0001 12:00:00 AM which is the min value of the DateeTime object, which means that the conversion failed(as per this documentation). The string input is parsed using formatting information in the current DateTimeFormatInfo object, which is supplied implicitly by the current thread culture. So the problem is the format of the date-string that you are passing.

Try using TryParseExact method in a smarter way since you know the format of the string input, like the following:

  string dateString ="24/10/2016 10:20";
  string formatString="dd/MM/yyyy HH:mm";
  DateTime dateValue;
  CultureInfo enUS = new CultureInfo("en-US"); // is up to you
  if (DateTime.TryParseExact(dateString, formatString , enUS, 
                                 DateTimeStyles.None, out dateValue))
0

The same code gives the correct output on my machine.

string dtstr = "24/10/2016 10:20";
DateTime outdt;
DateTime.TryParse(dtstr, out outdt);
Console.WriteLine(outdt);
Console.ReadLine();

You can look at the outdt variable

1
  • Please see my update. Exact same code produces error
    – l3utterfly
    Commented Oct 25, 2016 at 1:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.