1

I have a date coming from a database as a string. I want to set it to a datetimepicker.

The problem is, it can be in many formats, such as:

d/m/yyyy
d/mm/yyyy
dd/m/yyyy
dd/mm/yyyy

I don't know how to do it.

If I can convert this to something like dd/mm/yyyy I can show it in datetime picker.

How can I do this programmatically ?

thomasb
  • 5,816
  • 10
  • 57
  • 92
jayz
  • 401
  • 1
  • 5
  • 25
  • 1
    I think that these post will give you the answer you need: [Get Date Fomat From String][1] [1]: http://stackoverflow.com/questions/13513944/find-out-the-date-time-format-of-string-date-in-c-sharp – Y.S Jun 21 '15 at 05:12
  • 1
    People, if you're going to downvote a question, please at least leave a comment. Simply, downvoting someone is a pretty useless gesture. – Andy Evans Jun 23 '15 at 14:16

1 Answers1

1

You can parse it like that:

    string inputStringDate = "01/01/2001";
    DateTime outputDateTime;
    string[] formats = { "d/M/yyyy", "d/MM/yyyy", "dd/m/yyyy", "dd/mm/yyyy" };
    if (DateTime.TryParseExact(inputStringDate, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out outputDateTime))
    {
        //There you have your DateTime in outputDateTime var
    }
voytek
  • 2,202
  • 3
  • 28
  • 44