2013-05-01 25 views
2

正如您在下面看到的,我遇到了一個奇怪的行爲。奇怪的.NET DateTime問題(月份是00 ??)

我想實例化一年,一個月和一天的日期時間(很簡單!)。

但在調試模式下,Date顯示月份爲00,Month顯示爲4!

也許在我的解決方案中的配置問題?我在一個ASP .NET MVC 4應用程序中工作。

00 instead of 04

Date and Month differences

+1

這很奇怪。雖然內部的「Month」屬性是正確的,但它感覺像是一個調試顯示問題。 – 2013-05-01 19:43:19

+16

我的賭注是你的Windows文化設置使用小寫'mm'爲月份:) – Silvermind 2013-05-01 19:45:28

+0

嘗試新_DateTime(2013-04-24); _ – 2013-05-01 19:48:35

回答

8

這是因爲一個糟糕的格式字符串的地方,可能混淆"MM"(月)與"mm"(分鐘),如@Silvermind到的問題中留言說。我可以重現像這樣的問題:

var badCulture = new CultureInfo(""); 
badCulture.DateTimeFormat.ShortDatePattern = "dd-mm-yyyy"; 
Thread.CurrentThread.CurrentCulture = badCulture; 

var testDate = new DateTime(2013, 4, 24); // debug and rest mouse over variable 

因此,檢查某人是否在某處使用了錯誤的格式字符串。

+0

你是對的Jeppe! ShortDatePattern已在我的解決方案(!)中被覆蓋。非常感謝! :) – 2013-05-07 20:35:36