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); }
![](http://img-my.csdn.net/uploads/201304/03/1364974831_9091.png)