C#中獲取系統時間

        static void Main(string[] args)
        {
            Console.WriteLine("獲取日期加時間:");
            string strDateandTime = DateTime.Now.ToString();
            Console.WriteLine(strDateandTime);

            Console.WriteLine("獲取日期:");           
            string strDate1 = DateTime.Now.ToLongDateString();
            Console.WriteLine(strDate1);
            string strDate2 = DateTime.Now.ToShortDateString();
            Console.WriteLine(strDate2);
            string strDate3 = DateTime.Now.ToString("yyyy年MM月dd日");
            Console.WriteLine(strDate3);

            Console.WriteLine("獲取時間:");
            string strTime1 = DateTime.Now.ToLongTimeString();
            Console.WriteLine(strTime1);
            string strTime2 = DateTime.Now.ToShortTimeString();
            Console.WriteLine(strTime2);
            string strTime3 = DateTime.Now.ToString("hh時mm分ss秒");
            Console.WriteLine(strTime3);

            Console.Write("獲取年:");
            string strYear = DateTime.Now.Year.ToString();
            Console.WriteLine(strYear);

            Console.Write("獲取月:");
            string strMonth = DateTime.Now.Month.ToString();
            Console.WriteLine(strMonth);

            Console.Write("獲取日:");
            string strDate = DateTime.Now.DayOfYear.ToString();
            Console.WriteLine(strDate);

            Console.Write("獲取星期:");
            string strDay = DateTime.Now.DayOfWeek.ToString();
            Console.WriteLine(strDay);

            Console.Write("獲取時:");
            string strHour = DateTime.Now.Hour.ToString();
            Console.WriteLine(strHour);

            Console.Write("獲取分:");
            string strMinute = DateTime.Now.Minute.ToString();
            Console.WriteLine(strMinute);

            Console.Write("獲取秒:");
            string strSecond = DateTime.Now.Second.ToString();
            Console.WriteLine(strSecond);


            Console.WriteLine("按任意鍵結束……");
            Console.ReadKey(true);
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章