-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

Community
  • 1
  • 1
l3utterfly
  • 2,106
  • 4
  • 32
  • 58
  • How about use Regex to extract necessary numbers first? – Lei Yang Oct 25 '16 at 01:22
  • 2
    Could you please also show the piece of code you are running. – Mohit S Oct 25 '16 at 01:22
  • @MohitShrivastava the exact piece of code I'm running is irrelevant I think (I have given a specific example). But here it is if you want to see it: `DateTime.TryParse(res.StartDateString, out startdate);` – l3utterfly Oct 25 '16 at 01:23
  • 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 Oct 25 '16 at 01:24
  • @MohitShrivastava where `res.StartDateString` is the input in my question and `startdate` is the output – l3utterfly Oct 25 '16 at 01:24
  • Please use the edit link to add any further details rather than in the comments. – Dijkgraaf Oct 25 '16 at 01:25
  • 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 Oct 25 '16 at 01: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 Oct 25 '16 at 01:27
  • @Alex I can assume en-US – l3utterfly Oct 25 '16 at 01:28
  • If you can assume `en-US`, then `parseExact` should work perfectly. – Prisoner Oct 25 '16 at 01:31
  • Your code is running fine. – Mohit S Oct 25 '16 at 01:32
  • @Alex I don't know the exact format, I want to accommodate for other formats as well – l3utterfly Oct 25 '16 at 01:33
  • @MohitShrivastava how is it running fine? The output is not the right date time – l3utterfly Oct 25 '16 at 01:33
  • It is giving exact output on my machine. If you want I can post an answer to show the screenshot – Mohit S Oct 25 '16 at 01:34
  • @MohitShrivastava: huh? please post screenshot – l3utterfly Oct 25 '16 at 01:35
  • Hey @i3utterfly I have posted the answer. Please have a look. – Mohit S Oct 25 '16 at 01:43
  • 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. – Matt Johnson-Pint Oct 25 '16 at 01:49
  • 1
    Matt and Alex are correct, your code is working, the issue is you're expecting `en-us` but providing a date that is not in `en-us` (`mm/dd/yyyy`) format. The best you can do if you want to catch dates in either order, would be to first try parse `en-us`, if that fails, pass as pretty much any `dd/mm/yyyy` culture instead. – AllMadHare Oct 25 '16 at 02:37

2 Answers2

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))
Bharath
  • 195
  • 1
  • 1
  • 19
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
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

Mohit S
  • 13,723
  • 6
  • 34
  • 69