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 之前");

}

}

}

```


相关推荐
Lhan.zzZ10 小时前
Qt数据可视化实战:饼图、线图与表格的完整指南
开发语言·qt·信息可视化
Acrelhuang10 小时前
筑牢用电防线:Acrel-1000 自动化系统赋能 35kV 园区高效供电-安科瑞黄安南
java·大数据·开发语言·人工智能·物联网
小龙报10 小时前
《算法通关指南数据结构和算法篇(4)--- 队列和queue》
c语言·开发语言·数据结构·c++·创业创新·学习方法·visual studio
民乐团扒谱机10 小时前
深入浅出理解克尔效应(Kerr Effect)及 MATLAB 仿真实现
开发语言·matlab·光学·非线性光学·克尔效应·kerr effect
7澄110 小时前
深入解析 LeetCode 数组经典问题:删除每行中的最大值与找出峰值
java·开发语言·算法·leetcode·intellij idea
计算衎10 小时前
.c .o .a .elf .a2l hex map 这些后缀文件的互相之间的联系和作用
开发语言·elf·gcc·c/c++·a2l
ysyxg10 小时前
设计模式-策略模式
java·开发语言
m0_7482480211 小时前
C++与C#布尔类型深度解析:从语言设计到跨平台互操作
c++·stm32·c#
一抓掉一大把11 小时前
秒杀-StackExchangeRedisHelper连接单例
java·开发语言·jvm
星释11 小时前
Rust 练习册 :Minesweeper与二维数组处理
开发语言·后端·rust