2011-11-18 162 views
3

後,將字符串轉換爲DateTime我使用下面的代碼錯誤而發佈到Web服務器

DateTime dtt=new DateTime(); 
    dtt = Convert.ToDateTime(FromDate); 


    // DateTime dtt = DateTime.Parse(FromDate); //this also gives the same error 

    con = new MySqlConnection(conString); 
    con.Open(); 
    for (int i = 1; i <= TotalDays; i++) 
    {   
     string updateHotelBooking = "Update tbl_hotelbookingdetail set `BookedRoom`=`BookedRoom`+"+1+", `AvailableRoom`=`TotalRoom`-`BookedRoom` where `HotelID`="+HotelID+" AND `CurrentDate`='"+dtt.ToString("dd-MM-yyyy")+"'"; 
     MySqlCommand cmd7=new MySqlCommand(updateHotelBooking,con); 
     cmd7.ExecuteNonQuery();     
     dtt = dtt.AddDays(1); 
    } 

此代碼是在我的web服務的其中一個我使用iPhone應用程序。

這裏FromDate是字符串的值在這個合成15-11-2011這是來自應用程序的字符串格式。我將其轉換爲DateTime因爲在總天數 環I need to add day to dtt.

它正在本地主機與DTT值細15-11-2011 00:00:00

,但是當我發表它,它給了錯誤

String was not recognize as valid DateTime

+1

這可能有助於 的http://計算器。com/questions/2903339 /將問題轉換爲對象到datetime-in-c-sharp – Karthik

回答

5

這幾乎肯定是因爲你的服務器在默認情況下使用不同的文化 - 和你的代碼只是使用當前線程文化。

可以指定此使用DateTime.Parse - 或DateTime.ParseExactDateTime.TryParseExact明確指定模式 - 但我們需要更多地瞭解其中的字符串是來自於建議最好的辦法。它來自用戶嗎?如果是這樣,你應該使用用戶的文化來解析它。它是一種特定的格式(例如來自XML文檔)嗎?如果是這樣,請使用該特定格式進行解析。

理想的情況下,擺脫完全弦形部分的 - 如果你從數據庫中獲取它,例如,你可以存儲它把它拿來作爲一個代替DateTime作爲一個字符串?儘量減少涉及的字符串轉換次數是值得的。

編輯:從DD-MM-YYYY的固定格式解析,我會用:

DateTime value; 
if (DateTime.TryParseExact(text, "dd-MM-yyyy", 
          CultureInfo.InvariantCulture, 
          DateTimeStyles.AssumeUniversal, 
          out value)) 
{ 
    // Value will now be midnight UTC on the given date 
} 
else 
{ 
    // Parsing failed - invalid data 
} 
+0

更新我的問題,因爲你會問從字符串來自哪裏,它將以'dd-MM -yyyy'按照它將字符串 –

+0

@Ankit:我已經適當地編輯了答案。 –

0

記錄(或以其他方式向您自己提供反饋)FromDate是什麼。也許它是空的?

0

可在該服務器上的語言設置是不同的,因此它不能識別dd-MM-yyyy - 嘗試使用DateTime.ParseExact(dateString, "dd-MM-yyyy", CultureInfo.InvariantCulture);

1

服務器日期格式可能是MM/DD/YYYY,你試圖通過DD/MM/YYYY

using System; 
using System.Globalization; 

public class Example 
{ 
    public static void Main() 
    { 
     string[] dateValues = { "30-12-2011", "12-30-2011", 
           "30-12-11", "12-30-11" }; 
     string pattern = "MM-dd-yy"; 
     DateTime parsedDate; 

     foreach (var dateValue in dateValues) { 
     if (DateTime.TryParseExact(dateValue, pattern, null, 
            DateTimeStyles.None, out parsedDate)) 
      Console.WriteLine("Converted '{0}' to {1:d}.", 
           dateValue, parsedDate); 
     else 
      Console.WriteLine("Unable to convert '{0}' to a date and time.", 
           dateValue); 
     } 
    } 
} 
// The example displays the following output: 
// Unable to convert '30-12-2011' to a date and time. 
// Unable to convert '12-30-2011' to a date and time. 
// Unable to convert '30-12-11' to a date and time. 
// Converted '12-30-11' to 12/30/2011. 

Check this for more details

2

你是什麼你的本地計算機和服務器上的區域性設置?

DateTime轉換依賴於當前的文化 - 日期在不同的國家寫得相當不同。進行轉換 「predictible」

的一種方法是使用固定區域性:

DateTime dtt = Convert.ToDateTime(FromDate, CultureInfo.InvariantCulture);