Search code examples
c#.netwinformsdatetimepickerdatetime-parsing

How can i convert the string value to date time value and assign it to DateTimePicker


I stored a date value and retrieve it and the format of the date in the string is yyMMdd. Now when the user loads that from the string i would like to select the DateTimePicker as the user loaded date.

Sample code:

string strDate = strRead.Substring(23, 6);
DateTime dt = DateTime.Parse(strDate);

Can any one give me an idea for the next?


Solution

  • The most efficient way, I guess:

    DateTimePicker p = new DateTimePicker();
    DateTime result;
    if (DateTime.TryParseExact("101025", "yyMMdd", CultureInfo.CurrentCulture, DateTimeStyles.None, out result))
    {
        p.Value = result;
    }