2012-06-28 17 views
0

我知道在這個問題上有很多帖子,但沒有一個爲我工作,因此我想自己發佈一個帖子。我希望我不是垃圾郵件。在IIS上發佈網站後,無效的日期時間格式

我想要做的就是將字符串轉換爲DateTime對象。它在VS Web Develpoment Server上運行良好,但當我將它發佈到IIS時,它開始拋出異常「String未被識別爲有效的DateTime」。我經歷了無數帖子並嘗試了下面的代碼,但都失敗了。爲了簡單起見,我堅持使用DateTime字符串。請注意,字符串中DateTime的格式是固定的,我只能使用它。

Convert.ToDateTime("27-6-2012 9:05 PM") 

// Just for the sake of it I also used CurrentUICulture and InstalledUICulture in the line below. 
// Note: For me CurrentCulture = CurrentUICulture = InstalledUICulture = en-US 
Convert.ToDateTime("27-6-2012 9:05 PM", CultureInfo.CurrentCulture) 

DateTime.Parse("27-6-2012 9:05 PM") 

// Just for the sake of it I also used CurrentUICulture and InstalledUICulture in the line below. 
// Note: For me CurrentCulture = CurrentUICulture = InstalledUICulture = en-US 
DateTime.Parse("27-6-2012 9:05 PM", CultureInfo.CurrentCulture) 

// Just for the sake of it I also used CurrentUICulture and InstalledUICulture in the line below. 
// Note: For me CurrentCulture = CurrentUICulture = InstalledUICulture = en-US 
DateTime.ParseExact("27-6-2012 9:05 PM", "{0:d-M-yyyy h:mm tt}", CultureInfo.CurrentCulture) 

然後只是爲了體驗,我將字符串中的日期格式更改爲「2012年6月27日9:05 PM」。我發現除了DateTime.ParseExact以外的所有工作。對於我來說,如果任何一個上述代碼的工作,我很好,但問題是,我不能改變字符串中的日期格式,這是一個參數,這是不受我控制的。

這看起來像這樣一個微不足道的問題,但我不知道我錯過了什麼?這太尷尬了。任何人都可以幫助我嗎?

在此先感謝。

回答

0

服務器上的區域設置可能與您的開發環境不同。嘗試這個。 http://support.microsoft.com/kb/241671

編輯 - 查看區域設置下的控制面板。這將影響服務器如何讀取日期。

enter image description here

+0

感謝您的回覆。我早些時候來到這篇文章,但問題是,當我的應用程序投入生產時,我將不被允許進行這些更改。 – Aum

+0

只是爲了提供信息,我甚至將IIS上的Web應用程序的CurrentCulture和CurrentUICulture更改爲en-US。這也沒有幫助。在代碼中,當我調試時,它仍然將所有文化都顯示爲en-US。不知道發生了什麼問題! – Aum

+0

我剛纔嘗試了上述日期設置更改。仍然是同樣的問題。正如我在文章中提到的,如果我將日期格式的字符串更改爲「M/d/yyyy h:mm tt」,即如果我在示例中使用「2012年6月27日9:05 PM」,那麼情況會很好。我只是想讓這個點點亮! – Aum