您好,現在軟糖來為大家解答以上的問題。datetime生成時間序列,datetime格式相信很多小伙伴還不知道,現在讓我們一起來看看吧!
1、1. 默認情況下,DateTime.Now.ToString()的輸出與Control Panel中Date/Time的設置格式相關。
2、For example, 當Regional Options中Time設置:Time format: h:mm:ss ttAM symbol: 上午PM symbol:下午 Console.WriteLine(DateTime.Now.ToString());輸出結果:12/6/2004 2:37:37 下午 DateTime.Parse("12/6/2004 2:37:37 下午")OK // 將日期和時間的指定 String 表示形式轉換成其等效的 SqlDateTimeSqlDateTime.Parse("12/6/2004 2:37:37 下午")Exception:String was not recognized as a valid DateTime. SqlDateTime.Parse("12/6/2004 2:37:37 PM")OK 2. 通過DateTime.ToString(string format)方法,使用指定格式format將此實例的值轉換成其等效的字符串表示。
3、DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")輸出結果:12/06/2004 14:56:37此時,DateTime的輸出格式由format參數控制,與Regional Options中的Date/Time的設置無關。
4、不過,如果項目中有很多地方需要進行DateTime日期時間格式控制,這樣寫起來就比較麻煩,雖然可以通過常數const進行控制。
5、 3. 為當前線程的區域性創建 DateTimeFormatInfo。
6、// Sets the CurrentCulture property to U.S. English.System.Threading.Thread.CurrentThread.CurrentCulture = newCultureInfo("en-US", false); Console.WriteLine(DateTime.Now.ToString());輸出結果:12/6/2004 2:37:37 PM 若要為特定區域性創建 DateTimeFormatInfo,請為該區域性創建 CultureInfo 并檢索 CultureInfo.DateTimeFormat 屬性。
7、// Creates and initializes a DateTimeFormatInfo associated with the en-US culture.DateTimeFormatInfo myDTFI = new CultureInfo("en-US", false).DateTimeFormat; DateTimeFormatInfo 的實例可以針對特定區域性或固定區域性創建,但不能針對非特定區域性創建。
8、非特定區域性不提供顯示正確日期格式所需的足夠信息。
本文就為大家分享到這里,希望小伙伴們會喜歡。