有這行代碼:
DateTime inFutureDateTime = DateTime.Now.AddSeconds(60);
并像這樣在(Unity)控制臺中撰寫
Debug.Log("In future date time " inFutureDateTime.Date)
輸出:
12/15/2021 12:00:00 AM
日期是正確的(今天),但時間不是。它應該是 CurrentTime 60 秒。
我做錯了什么,還是我誤解了 DateTime 的作業原理。
提前致謝
uj5u.com熱心網友回復:
而不是.Date,嘗試呼叫.ToLongDateString()如Debug.Log("In future date time " inFutureDateTime.ToLongDateString())
uj5u.com熱心網友回復:
.Date 是僅用于 Date 的持有人。
不幸的是,當轉換為字串時,它也會顯示一個時間組件。
默認的 ToString 或其覆寫之一將顯示完整的日期時間:
Debug.Log("In future date time " inFutureDateTime)
uj5u.com熱心網友回復:
問題的根源在于.Date:它回傳初始值的日期部分DateTime(注意,時間部分是0:0:0.000)。我建議使用字串插值,您可以在其中指定文本、變數、它們的格式等。
// Here we specify the required format - MM/dd/yyyy HH:mm:ss
Debug.Log($"In future date time {inFutureDateTime:MM/dd/yyyy HH:mm:ss}");
如果要使用默認格式(來自CultureInfo.CurrentCulture):
Debug.Log($"In future date time {inFutureDateTime}");
uj5u.com熱心網友回復:
日期回傳組件日期不是日期格式標準。請參閱以下方法:
DateTime inFutureDateTime = DateTime.Now.AddSeconds(60);
Debug.Log("In future date time " inFutureDateTime.Date.ToLongDateString());
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/385951.html
下一篇:Unity中協程的例外處理
