DateTime的ToString方法格式


新建一個.NET Core控制台項目,敲入如下代碼:

using System;

namespace NetCoreDatetime
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime now = DateTime.Now;//當前時間:2019-09-25 15:56:56.673

            //yyyy年,MM月,dd日,HH時(24小時制),mm分,ss秒,fff毫秒
            string txtDate = now.ToString("yyyy-MM-dd HH:mm:ss.fff");//顯示:2019-09-25 15:56:56.673

            Console.WriteLine(txtDate);

            //yyyy年,MM月,dd日,hh時(12小時制),mm分,ss秒,fff毫秒
            txtDate = now.ToString("yyyy-MM-dd hh:mm:ss.fff");//顯示:2019-09-25 03:56:56.673

            Console.WriteLine(txtDate);

            Console.WriteLine("Press any key to end...");
            Console.ReadKey();
        }
    }
}

注意格式中,大寫HH和小寫hh的區別,大寫MM和小寫mm的區別,不要搞混淆。

 

參考文獻:

Get DateTime.Now with milliseconds precision

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM