-2

In my code I need to handle two types of datetime formats. if the input date is a like 8/31/2017 12:00:00 AM I just wanna save it. But when it comes with the format like "25.11.13" I wanna convert it like this 11/25/2013 12:00:00 AM and wanna save it.

Somehow I managed my code but the problem is the "else block" is not working as expected (actually it won't work at all).

DateTime registrationDate = new DateTime();

if (DateTime.TryParse(myTable.Rows[i][6].ToString(), out registrationDate))
{
    record.RegistrationDate = !string.IsNullOrEmpty(detailsTable.Rows[i][6].ToString()) ? Convert.ToDateTime(detailsTable.Rows[i][6].ToString()) : (DateTime?)null;
}
else
{
    DateTime.TryParse(detailsTable.Rows[i][6].ToString(), out registrationDate);
    record.RegistrationDate = registrationDate;
}
Thili
  • 748
  • 1
  • 9
  • 21
  • 1
    Possible duplicate of [Parse datetime in multiple formats](https://stackoverflow.com/questions/17859421/parse-datetime-in-multiple-formats) – Peter B Sep 28 '17 at 15:53
  • The else works fine, it's just that `25.11.13` can be converted by `TryParse` and so the `if` is always entered. – Equalsk Sep 28 '17 at 15:53
  • nope else block won't work. it shows value of registrationDate as 1/1/0001 12:00:00 AM when i pass 25.11.13 :-( – Thili Sep 28 '17 at 16:02
  • Well of course it wouldn't work, would it? Your `DateTime.TryParse()` in `if()` part and that which in `else` are the same. If you don't expect it to work in `if()` (which is why you have an `else` to begin with) then it wouldn't work in `else`. As your title suggests what you must do is use `TraParseExact()` in your else. – Sach Sep 28 '17 at 16:22
  • here a am getting two types of date format as i explained above so i want to check whether the datetime in 8/31/2017 12:00:00 AM in if bock so the tryParseExact() will return true ehh? if not i want to convert it. i am not muc clear the explanation could you bit explain with code – Thili Sep 28 '17 at 16:28
  • Martin Brown's explanation is a good one. Let me know if you can use that, otherwise I'll post an explanation of what I meant. – Sach Sep 28 '17 at 16:34
  • Hey @PeterB Please check the my Question before downvoting it's not same as you suggested it's totally different and the suggested one won't help me either – Thili Sep 28 '17 at 17:02

1 Answers1

3

I think you have a culture issue here. "8/31/2017" is clearly in US format M/D/Y because no month has 31 days. However "25.11.13" is in another D/M/Y (possibly UK) format.

Unless your detailsTable contains the culture the value is in you are stuck here. This is because it is impossible to tell if 9/11/2001 is the 9th of November or 11th of September without further information.

DateTime.TryParse will try to use the culture of the user that is running the code. Note if it is a web site then it runs as the account the IIS Service is set to use. There is an overload of TryParse that takes another parameter that allows you to tell it which culture the supplied string is in.

If you know that all dates that have slashes '/' are in US format and all dates that have dots '.' are in UK format then you can pre-parse the string to enable you to tell TryParse the correct culture. Like this:

static DateTime Parse(string input)
{
    CultureInfo ci =
        CultureInfo.CreateSpecificCulture(
            input.Contains(".") ?
                "en-GB" :
                "en-US");

    DateTime result;
    DateTime.TryParse(input, ci, DateTimeStyles.None, out result);        
    return result;
}
Martin Brown
  • 24,692
  • 14
  • 77
  • 122