我正在尝试根据用户选择的时间来学习如何安排图片。
以下是包含问题的代码:
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
}
}
}
答案 0 :(得分:9)
DateTimePicker.Value返回DateTime
个对象。您正在尝试不必要地转换为字符串类型和从字符串类型转换。
DateTime start = DateTimePickerStart.Value;
DateTime end = DateTimePickerEnd.Value;
答案 1 :(得分: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对象,因此您的代码可以简化,无需转换为字符串。