代码之家  ›  专栏  ›  技术社区  ›  Developer

如何将字符串值转换为日期时间值并将其分配给DateTimePicker

  •  0
  • Developer  · 技术社区  · 14 年前

    我存储了一个日期值并检索它,字符串中的日期格式是 yyMMdd . 现在,当用户从字符串中加载时,我想选择datetimepicker作为用户加载的日期。

    样本代码:

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

    有人能给我下一个主意吗?

    3 回复  |  直到 14 年前
        1
  •  4
  •   abatishchev Karl Johan    14 年前

    DateTimePicker p = new DateTimePicker();
    DateTime result;
    if (DateTime.TryParseExact("101025", "yyMMdd", CultureInfo.CurrentCulture, DateTimeStyles.None, out result))
    {
        p.Value = result;
    }
    
        2
  •  2
  •   Ohad Schneider    14 年前

    string strDate = strRead.Substring(23, 6);
    DateTime dt = DateTime.Parse(strDate);
    dateTimePicker.Value = dt;
    
        3
  •  1
  •   Itay Karo    14 年前
    dateTimePicker.Value = dt;
    dateTimePicker.Focus(); //if by Select to meant to give it the focus