2013-04-13 39 views
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 
     } 
    } 
} 
+2

我不明白你在做什麼,但'DateTimePicker1.Value'是'DateTime' – I4V

回答

9

DateTimePicker.Value返回DateTime對象。您正試圖不必要地將字符串類型轉換爲字符串類型。

DateTime start = DateTimePickerStart.Value; 
DateTime end = DateTimePickerEnd.Value; 
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對象本身,因此您的代碼可以簡化,無需轉換爲字符串。

+4

感謝您的意見。當我開始寫我的時候,他的回答沒有顯示出來。我是否慢一點降低了答案的價值? –

+2

+ 1因爲不是像Rohit那樣的小孩。 –

+0

非常感謝。 :) –