一、一般時間(常用的為黃色)
DateTime dt = DateTime.Now;
Console.WriteLine(dt.Date); //獲取日期
Console.WriteLine(dt.TimeOfDay); //獲取時間到毫秒:13:08:44.5278155
Console.WriteLine(dt.DayOfWeek); //星期,Monday等
Console.WriteLine((int)dt.AddDays(4).DayOfWeek); //數字星期1,2,3,4,5,6,0
Console.WriteLine(dt.DayOfYear); //一年中的第幾天
Console.WriteLine(dt.Kind); //輸出Local
Console.WriteLine(dt.Ticks); //單位是 100 毫微秒。表示自 0001 年 1 月 1 日午夜 12:00:00 以來已經過的時間的以 100 毫微秒為間隔的間隔數
Console.WriteLine(dt.Year); //年
Console.WriteLine(dt.Month); //月
Console.WriteLine(dt.Day); //一月中的第幾天
Console.WriteLine(dt.Hour); //小時
Console.WriteLine(dt.Minute); //分鍾
Console.WriteLine(dt.Second); //秒
Console.WriteLine(dt.Millisecond); //毫秒
Console.WriteLine(dt.Add(new TimeSpan(2,2,2,2)));//增加2天,2小時,2分中,2秒鍾,add里邊是TimeSpan()
Console.WriteLine(dt.AddYears(1)); //增加年
Console.WriteLine(dt.AddMonths(2)); //增加月
Console.WriteLine(dt.AddDays(2)); //增加天數
Console.WriteLine(dt.AddHours(2)); //增加小時
Console.WriteLine(dt.AddMinutes(10)); //增加分鍾
Console.WriteLine(dt.AddSeconds(300)); //增加秒
Console.WriteLine(dt.AddMilliseconds(600000)); //增加毫秒
Console.WriteLine(DateTime.MaxValue); //9999/12/31 23:59:59
Console.WriteLine(DateTime.MinValue); //0001/1/1 0:00:00
Console.WriteLine(DateTime.Today); //2018/11/21 0:00:00,和dt.Date類似
Console.WriteLine(DateTime.DaysInMonth(2018,11)); //返回指定月份的天數
Console.WriteLine(DateTime.IsLeapYear(2018)); //是否是閏年,false
二、根據時間獲取復雜時間
Console.WriteLine((dt.Month - 1)/3 + 1); //獲取季度
Console.WriteLine(dt.AddDays((int)dt.DayOfWeek > 0 ? 1 - (int)dt.DayOfWeek : 6)); //本周第一天
Console.WriteLine(dt.AddDays(1 - dt.Day)); //本月第一天
Console.WriteLine(dt.AddDays(1 - dt.DayOfYear)); //本年第一天
Console.WriteLine(new DateTime(DateTime.Now.Year, 1, 1)); //本年第一天
