【问题标题】:How can i convert the string value to date time value and assign it to DateTimePicker如何将字符串值转换为日期时间值并将其分配给 DateTimePicker
【发布时间】:2010-10-25 08:23:32
【问题描述】:

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

示例代码:

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

谁能给我下一个想法?

【问题讨论】:

  • 你是如何存储日期的?也许您不需要将其作为字符串返回。

标签: c# .net winforms datetimepicker datetime-parsing


【解决方案1】:

我猜是最有效的方法:

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

【讨论】:

    【解决方案2】:

    你是说

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

    【讨论】:

      【解决方案3】:
      dateTimePicker.Value = dt;
      dateTimePicker.Focus(); //if by Select to meant to give it the focus
      

      【讨论】:

        猜你喜欢
        • 2011-01-24
        • 2019-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-17
        • 2012-01-02
        相关资源
        最近更新 更多