在 C# 中,DateTime 类是用来处理日期和时间的类
# 设置日期和时间
| |
| DateTime dt1 = new DateTime(); |
| Console.WriteLine(dt1); |
| |
| |
| |
| DateTime dt2 = new DateTime(2024, 10, 30); |
| Console.WriteLine(dt2); |
| |
| |
| |
| DateTime dt3 = new DateTime(2024, 10, 30, 4, 16, 15); |
| Console.WriteLine(dt3); |
| |
| |
| |
| DateTime dt4 = new DateTime(2024, 10, 30, 4, 16, 15, DateTimeKind.Utc); |
| Console.WriteLine(dt4); |
# DateTime.Now
获取当前的日期和时间
# 属性
| |
| DateTime.Now |
| |
| |
| DateTime.Now.Ticks |
| |
| |
| DateTime.Now.Year |
| |
| |
| DateTime.Now.Month |
| |
| |
| DateTime.Now.Day |
| |
| |
| DateTime.Now.DayOfWeek |
| |
| |
| DateTime.Now.DayOfYear |
| |
| |
| DateTime.Now.TimeOfDay |
# 方法
| |
| DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") |
| |
| |
| DateTime.Now.ToShortDateString() |
| |
| |
| DateTime.Now.AddMinutes(1) |
| |
| |
| DateTime.Now.AddHours(1) |
| |
| |
| DateTime.Now.AddDays(1) |
| |
| |
| DateTime.Now.AddMonths(1) |
| |
| |
| DateTime.Now.AddYears(1) |
# DateTime
# 属性
| |
| DateTime currentDate = DateTime.Today; |
| |
| |
| DateTime date = new DateTime(2024, 04, 16, 04, 15, 16); |
| |
| long ticks = dateTime.Ticks; |
| |
| |
| int year = date.Year; |
| |
| |
| int month = date.Month; |
| |
| |
| int day = date.Day; |
| |
| |
| int dayOfWeek = date.DayOfWeek; |
| |
| |
| int dayOfYear = date.DayOfYear; |
| |
| |
| TimeSpan timeOfDay = dateTime.TimeOfDay; |
| |
| |
| DateTimeKind kind = dateTime.Kind; |
| DateTime date2 = DateTime.Now; |
| Console.WriteLine(date2.Kind); |
# 方法
| |
| DateTime date1 = new DateTime(2024, 10, 21); |
| DateTime date2 = new DateTime(2024, 10, 22); |
| int result = DateTime.Compare(date1, date2); |
| Console.WriteLine(result); |
| |
| |
| |
| DateTime date1 = new DateTime(2024, 10, 21); |
| DateTime date2 = new DateTime(2024, 10, 21); |
| bool isEqual = DateTime.Equals(date1, date2); |
| Console.WriteLine(isEqual); |
| |
| |
| |
| int year = 2024; |
| bool isLeapYear = DateTime.IsLeapYear(year); |
| Console.WriteLine(isLeapYear); |
| |
| |
| |
| DateTime dateTime = new DateTime(2024, 10, 30, 4, 16, 0); |
| string dateString = dateTime.ToString("yyyy-MM-dd HH:mm:ss"); |
| Console.WriteLine(dateString); |
| |
| |
| |
| string dateString = "2024-10-30"; |
| DateTime date; |
| bool success = DateTime.TryParse(dateString, out date); |
| if (success) |
| { |
| Console.WriteLine(date); |
| } |
| else |
| { |
| Console.WriteLine("Invalid date format"); |
| } |
# Ticks
| DateTime dt = new DateTime(636370000000000000); |
| Console.WriteLine(DateTime.MinValue.Ticks); |
| Console.WriteLine(DateTime.MaxValue.Ticks); |
# TimeSpan 时间跨度
| DateTime dt = new DateTime(2024, 10, 30, 4, 16, 15); |
| TimeSpan ts = new TimeSpan(4, 16, 15); |
| DateTime newDate = dt.Add(ts); |
| Console.WriteLine(newDate); |
两个日期相减会得出 TimeSpan
| DateTime dt1 = new DateTime(2024, 10, 30, 8, 32, 30); |
| DateTime dt2 = new DateTime(2024, 10, 30, 4, 16, 15); |
| TimeSpan result1 = dt1.Subtract(dt2); |
| TimeSpan result2 = dt2.Subtract(dt1); |
| Console.WriteLine(result1); |
| Console.WriteLine(result2); |
# DateTime 转换为 String
方法 | 描述 |
---|
ToString | 将 DateTime 值转换为当前区域性的指定格式的字符串 |
ToShortDateString | 将 DateTime 值转换为当前区域内的短日期字符串 (M/d/yyyy 模式) |
ToShortTimeString | 将 DateTime 值转换为当前区域性中的短时间字符串 (h: mm: ss 模式) |
ToLongDateString | 将 DateTime 值转换为当前区域性的长日期字符串 (dddd, MMMM d, yyyy 模式) |
ToLongTimeString | 将 DateTime 值转换为当前区域内的长时间字符串 (h:mm:ss tt 模式) |