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

}

}

}

```


相关推荐
努力努力再努力wz3 小时前
【Qt入门系列】:按钮组件全解析:从 QAbstractButton 到快捷键事件、单选与复选机制
c语言·开发语言·数据结构·c++·git·qt·github
skywalk81633 小时前
言知(Yanzhi)系统提升建议报告和完工报告 by AutoCoder
开发语言·编程
yunn_4 小时前
单例模式两种实现方法
开发语言·c++·单例模式
我材不敲代码4 小时前
Python基础:列表详解、增删改查及常用高阶操作
开发语言·windows·python
AI玫瑰助手4 小时前
Python运算符:成员运算符(in/not in)的使用场景
开发语言·python·信息可视化
AI人工智能+电脑小能手4 小时前
【大白话说Java面试题 第77题】【Mysql篇】第7题:回表查询与全表扫描的区别?
java·开发语言·数据库·mysql·面试
水木流年追梦4 小时前
大模型入门-大模型分布式训练2
开发语言·分布式·python·算法·正则表达式·prompt
口袋里のInit5 小时前
基础知识——ARM M核入栈出栈流程
开发语言·arm开发
罗超驿5 小时前
5.Java线程创建全攻略:5种写法 + 高频面试题解析
java·开发语言·java-ee