C# 零基础入门篇(19.DateTime 使用指南)

一、概述

`DateTime` 是 C# 中用于表示日期和时间的结构,位于 `System` 命名空间中。它提供了丰富的属性和方法,用于处理日期和时间的创建、格式化、比较和计算。

二、创建 DateTime 对象

(一)使用默认构造函数

```

DateTime now = DateTime.Now; // 获取当前日期和时间

DateTime today = DateTime.Today; // 获取当前日期,时间为 00:00:00

```

(二)指定日期和时间

```

DateTime specificDate = new DateTime(2025, 3, 5, 14, 30, 0); // 年、月、日、时、分、秒

Console.WriteLine(specificDate.ToString()); // 输出:2025-03-05 14:30:00

```

(三)从字符串解析

```

string dateString = "2025-03-05";

DateTime parsedDate = DateTime.Parse(dateString); // 解析标准格式的日期字符串

Console.WriteLine(parsedDate.ToString()); // 输出:2025-03-05 00:00:00

string dateTimeString = "2025-03-05 14:30:00";

DateTime parsedDateTime = DateTime.Parse(dateTimeString);

Console.WriteLine(parsedDateTime.ToString()); // 输出:2025-03-05 14:30:00

```

三、DateTime 的常用属性

(一)获取日期部分

```

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.DayOfWeek); // 输出星期几

Console.WriteLine(now.DayOfYear); // 输出一年中的第几天

```

四、格式化 DateTime

(一)标准格式化

```

DateTime now = DateTime.Now;

Console.WriteLine(now.ToString("yyyy-MM-dd")); // 输出:2025-03-05

Console.WriteLine(now.ToString("yyyy-MM-dd HH:mm:ss")); // 输出:2025-03-05 14:30:00

Console.WriteLine(now.ToString("yyyy年MM月dd日")); // 输出:2025年03月05日

```

(二)自定义格式化

```

Console.WriteLine(now.ToString("dddd, dd MMMM yyyy")); // 输出:星期三, 05 March 2025

Console.WriteLine(now.ToString("HH:mm:ss")); // 输出:14:30:00

```

五、计算日期和时间

(一)日期加减

```

DateTime now = DateTime.Now;

DateTime tomorrow = now.AddDays(1); // 加一天

DateTime yesterday = now.AddDays(-1); // 减一天

Console.WriteLine(tomorrow.ToString("yyyy-MM-dd")); // 输出:2025-03-06

Console.WriteLine(yesterday.ToString("yyyy-MM-dd")); // 输出:2025-03-04

```

(二)时间加减

```

DateTime now = DateTime.Now;

DateTime nextHour = now.AddHours(1); // 加一小时

DateTime previousHour = now.AddHours(-1); // 减一小时

Console.WriteLine(nextHour.ToString("HH:mm:ss")); // 输出:15:30:00

Console.WriteLine(previousHour.ToString("HH:mm:ss")); // 输出:13:30:00

```

(三)计算两个日期之间的差异

```

DateTime startDate = new DateTime(2025, 1, 1);

DateTime endDate = DateTime.Now;

TimeSpan difference = endDate - startDate; // 计算时间差

Console.WriteLine(difference.Days); // 输出天数差异

Console.WriteLine(difference.TotalHours); // 输出总小时数差异

```

六、比较 DateTime

(一)使用比较运算符

```

DateTime date1 = new DateTime(2025, 3, 5);

DateTime date2 = new DateTime(2025, 3, 6);

if (date1 < date2)

{

Console.WriteLine("date1 在 date2 之前");

}

else if (date1 > date2)

{

Console.WriteLine("date1 在 date2 之后");

}

else

{

Console.WriteLine("date1 和 date2 相同");

}

```

(二)使用 CompareTo 方法

```

int comparison = date1.CompareTo(date2);

if (comparison < 0)

{

Console.WriteLine("date1 在 date2 之前");

}

else if (comparison > 0)

{

Console.WriteLine("date1 在 date2 之后");

}

else

{

Console.WriteLine("date1 和 date2 相同");

}

```

七、注意事项

  1. **时区问题**:`DateTime` 默认为本地时区,如果需要处理 UTC 时间,可以使用 `DateTime.UtcNow` 或 `DateTime.ToUniversalTime()` 方法。

  2. **格式化字符串**:在格式化日期和时间时,确保格式字符串符合规范,避免出现错误。

  3. **性能优化**:`DateTime` 是一个结构体,操作效率较高,但在大量日期计算时,注意优化逻辑以避免性能问题。

  4. **线程安全**:`DateTime` 是不可变的,因此是线程安全的。

八、示例代码

以下是一个完整的示例代码,展示 `DateTime` 的常见用法:

```

using System;

class Program

{

static void Main()

{

// 获取当前日期和时间

DateTime now = DateTime.Now;

Console.WriteLine("当前日期和时间:" + now.ToString("yyyy-MM-dd HH:mm:ss"));

// 格式化日期

Console.WriteLine("格式化日期:" + now.ToString("yyyy年MM月dd日 HH时mm分ss秒"));

// 计算日期

DateTime tomorrow = now.AddDays(1);

Console.WriteLine("明天的日期:" + tomorrow.ToString("yyyy-MM-dd"));

// 计算时间差

DateTime startDate = new DateTime(2025, 1, 1);

TimeSpan difference = now - startDate;

Console.WriteLine("从2025年1月1日到现在的天数:" + difference.Days);

// 比较日期

DateTime date1 = new DateTime(2025, 3, 5);

DateTime date2 = new DateTime(2025, 3, 6);

if (date1 < date2)

{

Console.WriteLine("date1 在 date2 之前");

}

}

}

```


相关推荐
吃喝不愁霸王餐APP开发者5 分钟前
基于Spring Cloud Gateway实现对外卖API请求的统一鉴权与流量染色
java·开发语言
心疼你的一切1 小时前
三菱FX5U PLC与C#通信开发指南
开发语言·单片机·c#
Tim_101 小时前
【C++入门】04、C++浮点型
开发语言·c++
@淡 定1 小时前
Java内存模型(JMM)详解
java·开发语言
谈笑也风生1 小时前
经典算法题型之复数乘法(二)
开发语言·python·算法
hkNaruto1 小时前
【C++】记录一次C++程序编译缓慢原因分析——滥用stdafx.h公共头文件
开发语言·c++
czhc11400756632 小时前
C# 1221
java·servlet·c#
先知后行。2 小时前
python的类
开发语言·python
派大鑫wink2 小时前
【Day12】String 类详解:不可变性、常用方法与字符串拼接优化
java·开发语言
JIngJaneIL2 小时前
基于springboot + vue健康管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·后端