在C#编程中,DateTime是处理日期和时间的核心类型,位于System命名空间下,支持日期创建、属性取值、格式化输出、时间运算、时区转换等多种操作,是日常开发中使用频率极高的基础类型。

一、DateTime的创建方式
DateTime提供了多种构造方式,可根据实际需求创建对应的日期时间实例。
1. 基础构造方法
通过指定年、月、日、时、分、秒等参数创建DateTime实例,最常用的构造方法如下:
using System;
class Program
{
static void Main()
{
// 创建2024年5月1日 12点30分15秒的DateTime实例
DateTime dt1 = new DateTime(2024, 5, 1, 12, 30, 15);
Console.WriteLine(dt1);
// 仅指定年月日,时分秒默认为0
DateTime dt2 = new DateTime(2024, 5, 1);
Console.WriteLine(dt2);
}
}
2. 获取当前时间
通过DateTime.Now可以获取当前本地时间,DateTime.UtcNow可以获取当前的UTC时间:
using System;
class Program
{
static void Main()
{
// 获取当前本地时间
DateTime localNow = DateTime.Now;
Console.WriteLine($"本地当前时间:{localNow}");
// 获取当前UTC时间
DateTime utcNow = DateTime.UtcNow;
Console.WriteLine($"UTC当前时间:{utcNow}");
}
}
3. 解析字符串为DateTime
如果有一个日期时间格式的字符串,可以通过DateTime.Parse或DateTime.TryParse方法转换为DateTime实例,后者可以避免转换失败抛出异常:
using System;
class Program
{
static void Main()
{
string timeStr = "2024-05-01 12:30:15";
// Parse方法,转换失败会抛异常
DateTime dt3 = DateTime.Parse(timeStr);
Console.WriteLine(dt3);
// TryParse方法,转换失败不会抛异常,返回bool结果
if (DateTime.TryParse("2024-05-01", out DateTime dt4))
{
Console.WriteLine($"转换成功:{dt4}");
}
else
{
Console.WriteLine("转换失败");
}
}
}
二、DateTime常用属性取值
DateTime提供了丰富的属性,可以获取日期时间的各个组成部分:
using System;
class Program
{
static void Main()
{
DateTime now = DateTime.Now;
Console.WriteLine($"当前年份:{now.Year}");
Console.WriteLine($"当前月份:{now.Month}");
Console.WriteLine($"当前日:{now.Day}");
Console.WriteLine($"当前小时:{now.Hour}");
Console.WriteLine($"当前分钟:{now.Minute}");
Console.WriteLine($"当前秒:{now.Second}");
Console.WriteLine($"当前毫秒:{now.Millisecond}");
Console.WriteLine($"当前是星期几:{now.DayOfWeek}");
Console.WriteLine($"当前是今年的第几天:{now.DayOfYear}");
Console.WriteLine($"当前日期部分:{now.Date}");
}
}
三、DateTime格式化输出
可以通过ToString方法配合格式字符串,将DateTime转换为指定格式的字符串,常用的格式符如下:
| 格式符 | 说明 | 示例输出 |
|---|---|---|
| yyyy | 四位年份 | 2024 |
| MM | 两位月份,不足补0 | 05 |
| dd | 两位日期,不足补0 | 01 |
| HH | 24小时制小时,两位不足补0 | 14 |
| hh | 12小时制小时,两位不足补0 | 02 |
| mm | 两位分钟,不足补0 | 30 |
| ss | 两位秒,不足补0 | 15 |
| fff | 三位毫秒 | 123 |
格式化代码示例:
using System;
class Program
{
static void Main()
{
DateTime dt = new DateTime(2024, 5, 1, 14, 30, 15, 123);
// 自定义格式输出
string format1 = dt.ToString("yyyy-MM-dd HH:mm:ss.fff");
Console.WriteLine(format1); // 输出 2024-05-01 14:30:15.123
string format2 = dt.ToString("yyyy年MM月dd日 hh:mm:ss");
Console.WriteLine(format2); // 输出 2024年05月01日 02:30:15
// 使用内置标准格式
Console.WriteLine(dt.ToString("d")); // 短日期格式,输出 2024/5/1
Console.WriteLine(dt.ToString("D")); // 长日期格式,输出 2024年5月1日
Console.WriteLine(dt.ToString("F")); // 完整日期短时间格式,输出 2024年5月1日 14:30
}
}
四、DateTime时间运算
DateTime支持加减时间跨度,时间跨度通过TimeSpan类型表示,也可以直接对日期的各个部分进行增减。
1. 加减TimeSpan
using System;
class Program
{
static void Main()
{
DateTime dt = new DateTime(2024, 5, 1, 12, 0, 0);
// 加3天2小时
TimeSpan addSpan = new TimeSpan(3, 2, 0, 0);
DateTime newDt1 = dt.Add(addSpan);
Console.WriteLine($"加3天2小时后:{newDt1}");
// 减1小时30分钟
DateTime newDt2 = dt.Subtract(new TimeSpan(1, 30, 0));
Console.WriteLine($"减1小时30分钟后:{newDt2}");
}
}
2. 直接增减指定时间单位
DateTime提供了AddYears、AddMonths、AddDays等专用方法,可以直接对对应单位进行增减:
using System;
class Program
{
static void Main()
{
DateTime dt = new DateTime(2024, 5, 1, 12, 0, 0);
Console.WriteLine($"原时间:{dt}");
Console.WriteLine($"加2年:{dt.AddYears(2)}");
Console.WriteLine($"减3个月:{dt.AddMonths(-3)}");
Console.WriteLine($"加10天:{dt.AddDays(10)}");
Console.WriteLine($"减2小时:{dt.AddHours(-2)}");
Console.WriteLine($"加30分钟:{dt.AddMinutes(30)}");
}
}
3. 计算两个时间的间隔
两个DateTime实例相减,会得到TimeSpan类型的间隔结果:
using System;
class Program
{
static void Main()
{
DateTime start = new DateTime(2024, 5, 1, 8, 0, 0);
DateTime end = new DateTime(2024, 5, 3, 12, 30, 0);
TimeSpan span = end - start;
Console.WriteLine($"间隔总天数:{span.TotalDays}");
Console.WriteLine($"间隔总小时数:{span.TotalHours}");
Console.WriteLine($"间隔总分钟数:{span.TotalMinutes}");
Console.WriteLine($"间隔总秒数:{span.TotalSeconds}");
}
}
五、DateTime时区转换
如果需要处理不同时区的时间,可以结合TimeZoneInfo类完成转换:
using System;
class Program
{
static void Main()
{
// 获取UTC时间
DateTime utcNow = DateTime.UtcNow;
Console.WriteLine($"UTC时间:{utcNow}");
// 转换为东八区时间(北京时间)
TimeZoneInfo chinaZone = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time");
DateTime chinaTime = TimeZoneInfo.ConvertTimeFromUtc(utcNow, chinaZone);
Console.WriteLine($"北京时间:{chinaTime}");
// 本地时间转换为UTC时间
DateTime localNow = DateTime.Now;
DateTime localToUtc = TimeZoneInfo.ConvertTimeToUtc(localNow);
Console.WriteLine($"本地时间转UTC:{localToUtc}");
}
}
六、注意事项
- DateTime的
Now属性获取的是本地时间,受系统时区设置影响,如果需要统一的时间标准,建议使用UtcNow。 - 使用
DateTime.Parse转换字符串时,如果字符串格式不符合当前系统的日期格式,会抛出异常,生产环境建议使用DateTime.TryParse或者指定格式的DateTime.ParseExact。 - DateTime类型是可变的,所有Add相关方法都会返回新的DateTime实例,不会修改原实例的值。
- 如果需要处理更复杂的时间场景,比如只有日期没有时间、只有时间没有日期,可以考虑使用
DateOnly和TimeOnly类型,这两个类型是.NET 6之后新增的,更贴合对应场景的需求。