簡體   English   中英

日期時間 TryParse 准確返回 0001:01:01

[英]DateTime TryParse Exact returning 0001:01:01

我正在嘗試使用方法解析 DateTime 字符串'28/3/2014

DateTime d;
DateTime.TryParseExact("28/3/2014", "dd'/'MM'/'yyyy",
                       System.Globalization.CultureInfo.InvariantCulture,
                       System.Globalization.DateTimeStyles.None,
                       out d);

它總是返回我0001:01:01

你能告訴如何設置轉換參數嗎? 我試過設置

System.Globalization.CultureInfo.CurrentCulture
System.Globalization.DateTimeStyles.AssumeLocal

沒有效果

28/3/2014就是一個例子。 日期也可能是28/12/2014

你的模式必須是

"dd'/'M'/'yyyy"

有關更多信息,請參閱自定義日期和時間格式字符串一文

您的輸入字符串月份沒有前導 0,因此您可以嘗試以下操作:

DateTime d;
DateTime.TryParseExact("28/03/2014", "dd/MM/yyyy", 
                        System.Globalization.CultureInfo.InvariantCulture,
                        System.Globalization.DateTimeStyles.None, out d);

或者

DateTime d;
DateTime.TryParseExact("28/3/2014", "dd/M/yyyy", 
                        System.Globalization.CultureInfo.InvariantCulture,
                        System.Globalization.DateTimeStyles.None, out d);

哈姆雷特 Hakobyans 的回答是正確的。 但我想至少添加一些解釋。

讓我們逐步分析您的問題:

你說你的字符串是'28/3/2014但你在你的例子中使用了28/3/2014 我認為,28/3/ 28/3/2014是適合您的情況的字符串。

所以,你真正的代碼是;

DateTime d;
DateTime.TryParseExact("28/3/2014", "dd'/'MM'/'yyyy", 
                        CultureInfo.InvariantCulture, 
                        System.Globalization.DateTimeStyles.None,
                        out d);

在這種情況下,您不需要使用'作為/分隔符,但這不會破壞您的代碼。 因為'是一個文字字符串分隔符,對於這種情況它仍然有效。

但是MM說明符用於0112 ,這不適合3月份值。 您應該使用M說明符,它是112

它總是讓我回來 0001:01:01

因為您的TryParseExact返回false (因為您的格式不適合您的字符串)並且來自它的文檔

結果

類型:System.DateTime

當此方法返回時,如果轉換成功,則包含與 s 中包含的日期和時間等效的 DateTime 值,如果轉換失敗,則包含MinValue

0001:01:01等於DateTime.MinValue字段(以及其他部分也......)。 這就是您獲得此值的原因。

最后,你的代碼應該是;

DateTime d;
DateTime.TryParseExact("28/3/2014", "dd/M/yyyy",
                        CultureInfo.InvariantCulture,
                        DateTimeStyles.None,
                        out d);

如果你想要28/3/2014作為stringDateTime沒有格式)結果,你有幾個選擇;

如果您當前的線程文化的DateSeperator是 / 並且ShortDatePatterndd/m/yyyy ,則您不需要使用任何東西。 只是;

Console.WriteLine(d); //This will be enough.

如果不是,您可以使用DateTime.ToString(String, IFormatProvider)InvariantCulture 重載,例如;

Console.WriteLine(d.ToString("dd/M/yyyy", CultureInfo.InvariantCulture)); 

或者你可以逃避你的/無論你使用哪種文化;

Console.WriteLine(d.ToString("dd'/'M'/'yyyy"));

或者

Console.WriteLine(d.ToString(@"dd\/M\/yyyy"));

嘗試這個:

DateTime d;
DateTime.TryParseExact("28/3/2014", "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out d);

問題是月份輸入數字的格式。 您已將格式設置為 "MM" 但您僅作為參數傳遞 3 。 嘗試 28/03/2014(“03”而不是簡單的“3”)。 順便說一下,格式字符串可以簡單地為“dd/MM/yyyy”而不是“dd'/'MM'/'yyyy”。 http://dotnetfiddle.net/I3Dyod上檢查

我不小心離開了秒部分並試圖翻譯這個: DateTime.TryParse(@"1/1/2000 00:00:000", out DateTime dtLastRun);

它應該是: DateTime.TryParse(@"1/1/2000 00:00:00:000", out DateTime dtLastRun);

我不明白為什么 DateTime 返回 DateTime2 響應。 但無論如何,如果您有一個無效的字符串,您將獲得返回的默認時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM