0

我正在尝试学习如何根据用户选择的时间安排图片。

这是有问题的代码:

private void startjob()
{
     string theDate = DateTimePicker1.Value.ToString();
     DateTime dt = Convert.ToDateTime(date);
     {
         DateTime start = new DateTime(2009, 12, 9, 10, 0, 0); //How Do I make this to read the string that is converted from DateTimePicker instead of this?
         DateTime end = new DateTime(2009, 12, 10, 12, 0, 0); //How Do I make this to read the string that is converted from DateTimePicker instead of this?
         DateTime now = DateTime.Now;
         if ((now > start) && (now < end))
         {
         //match found
         }
     }
}
4

2 回答 2

9

DateTimePicker.Value返回DateTime对象。您正在尝试不必要地转换为字符串类型。

DateTime start = DateTimePickerStart.Value;
DateTime end = DateTimePickerEnd.Value;
于 2013-04-13T20:01:40.480 回答
3

假设您的控件被命名为 DateTimePicker1 和 DateTimePicker2:

private void startjob()
{
    DateTime start = DateTimePicker1.Value;
    DateTime end = DateTimePicker2.Value;
    DateTime now = DateTime.Now;
    if ((now > start) && (now < end))
    {
    //match found
    }
}

DateTimePicker.Value 属性本身就是一个 DateTime 对象,因此您的代码可以简化,无需转换为字符串。

于 2013-04-13T20:05:00.347 回答