2014-01-24 42 views
0

嗨我無法從雙轉換datetime錯誤時運行我的ASP.NET MVC代碼。無效的從雙日期時間錯誤轉換

這是我的代碼:

更新:嗨,我下面將我的全部代碼。請看看這個。

 Boolean locked = false; 
     if (frmcollection["lockStart"] != null && frmcollection["lockStart"] != "") 
     { 
      locked = Convert.ToBoolean(frmcollection["lockStart"].ToString()); 

     } 
     else if (datelock == "") 
     { 
      locked = Convert.ToBoolean("0"); 
     } 
     Boolean valid = true; 
     double inteval = 86400000 * Convert.ToDouble(frmcollection["autoFrequency"].ToString()); 

     DateTime schedulestartDate = Convert.ToDateTime(frmcollection["autoStart"].ToString()); 

     int startHour = Convert.ToInt32(frmcollection["autoStartHour"].ToString()); 
     DateTime sd = schedulestartDate; 

     sd.AddHours(startHour); 


     DateTime filterStart = Convert.ToDateTime(frmcollection["periodStart"].ToString()); 

     int filterStartHour = Convert.ToInt32(frmcollection["periodStartHour"].ToString()); 
     DateTime fsd = filterStart; 

     fsd.AddHours(filterStartHour); 

     DateTime filterEnd = Convert.ToDateTime(frmcollection["periodEnd"].ToString()); 

     int filterEndHour = Convert.ToInt32(frmcollection["periodEndHour"].ToString()); 
     DateTime fed = filterEnd; 

     fed.AddHours(filterEndHour); 


     double sDate = sd.Second; 
     double sPeriod = sDate - fsd.Second; 
     double ePeriod = sDate - fed.Second; 

     if (sPeriod < ePeriod || sPeriod < 0 || ePeriod < 0) 
     { 
      valid = false; 
     } 

     if (valid) 
     { 
      for (int i = 0; i < 5; i++) 
      { 
      DateTime date = Convert.ToDateTime(sDate + (inteval * i)); 

       if (locked) 
       { 
        DateTime psdate = Convert.ToDateTime(sDate - sPeriod); 
       } 
       else 
       { 
        DateTime psdate = Convert.ToDateTime(sDate + (inteval * i) - sPeriod); 
       } 
       DateTime pedate = Convert.ToDateTime(sDate + (inteval * i) - ePeriod); 

      } 
     } 
     else 
     { 

     } 

當我調試,我剛開錯誤在這行:

DateTime date = Convert.ToDateTime(sDate + (inteval * i)); 

有人可以幫我在這?

+0

您的值看起來像是'TimeSpan'的一部分,您希望如何將某些秒轉換爲'DateTime'類型的對象? – Habib

+1

您是否嘗試過使用DateTime.FromSeconds? – shirbr510

+0

@ shirbr510,你在哪裏看到'DateTime.FromSeconds'?我想你的意思是'TimeSpan.FromSeconds' – Habib

回答

2

您正在將double添加到interval * i解決的任何問題。

您無法將其轉換爲DateTime,這正是錯誤告訴您的。

+0

嗨,我編輯了我的文章。我粘貼我的完整代碼。有一個看看,如果你不介意可以告訴我這個解決方案? – Ajay

+0

分配後,我沒有看到你使用'date'的位置,所以我不確定它的意圖是什麼。它看起來像'inteval'是一天中的毫秒數,但隨後你乘以一個任意數字,那麼你想在日期中添加什麼?秒?毫秒? –

1

看來,如果你正在尋找的日期「SD」經過一番(間隔* I)秒的日期。如果是這樣,請嘗試:

for (int i = 0; i < 5; i++) 
{ 
    DateTime date = sd.AddSeconds(inteval * i); 

    if (locked) 
    { 
       DateTime psdate = sd.AddSeconds(-sPeriod); 
      } 
      else 
      { 
       DateTime psdate = sd.AddSeconds((inteval * i) - sPeriod)); 
      } 
      DateTime pedate = sd.AddSeconds((inteval * i) - ePeriod); 
    } 
    //... 
+0

嗨你的代碼運行良好,但我認爲這個邏輯有問題。如果我使用你的代碼,我會得到意想不到的結果 – Ajay

+0

@Ajay你期望得到什麼結果,你得到了什麼結果? –

+0

@Ajay'double inteval = 86400000 * Convert.ToDouble(frmcollection [「autoFrequency」]。ToString());' - 它看起來像是用ms而不是秒。 –

相關問題