C#开发——日期操作类DateTime

embedded/2025/3/1 21:59:40/

在C#中,日期和时间的操作主要通过  System.DateTime  类来实现。  DateTime  提供了丰富的属性和法,用于处理日期和时间的创建、格式化、比较和计算等操作。以下是一些常用的日期函数和特性:

一、创建日期和时间

1、直接指定日期和时间:

DateTime now = DateTime.Now; // 获取当前日期和时间
DateTime today = DateTime.Today; // 获取当前日期(时间部分为00:00:00)
DateTime specificDate = new DateTime(2025, 2, 26, 14, 30, 0); // 指定具体日期和时间

2、从字符串解析日期和时间:

DateTime parsedDate = DateTime.Parse("2025-02-26 14:30:00"); // 从标准格式字符串解析
DateTime parsedDateWithFormat = DateTime.ParseExact("26/02/2025 14:30", "dd/MM/yyyy HH:mm", null); // 使用自定义格式解析

二、获取日期和时间的组成部分

DateTime  提供了多个只读属性,用于获取日期和时间的各个部分:

  1. Year  :获取年份。
  2. Month  :获取月份(1-12)。
  3. Day  :获取日期(1-31)。
  4. Hour  :获取小时(0-23)。
  5. Minute  :获取分钟(0-59)。
  6. Second  :获取秒(0-59)。

示例:

DateTime now = DateTime.Now;
Console.WriteLine($"Year: {now.Year}, Month: {now.Month}, Day: {now.Day}");
Console.WriteLine($"Hour: {now.Hour}, Minute: {now.Minute}, Second: {now.Second}");

三、日期和时间的计算

1、加减日期和时间:

DateTime now = DateTime.Now;
DateTime tomorrow = now.AddDays(1); // 加1天
DateTime yesterday = now.AddDays(-1); // 减1天
DateTime nextWeek = now.AddWeeks(1); // 加1周(需要扩展方法)
DateTime nextHour = now.AddHours(1); // 加1小时

2、计算两个日期之间的差异:

DateTime date1 = new DateTime(2025, 2, 26);
DateTime date2 = new DateTime(2025, 3, 1);
TimeSpan difference = date2 - date1; // 返回TimeSpan对象
Console.WriteLine($"Days: {difference.Days}, Hours: {difference.Hours}");

四、格式化日期和时间

1、标准格式化:

DateTime now = DateTime.Now;
string formattedDate = now.ToString("yyyy-MM-dd HH:mm:ss"); // 自定义格式
string shortDate = now.ToShortDateString(); // 短日期格式(如:2025/02/26)
string longDate = now.ToLongDateString(); // 长日期格式(如:2025年2月26日)

2、自定义格式化:

string customFormat = now.ToString("dd/MM/yyyy HH:mm:ss tt"); // 自定义格式(如:26/02/2025 14:30:00 PM)

五、比较日期和时间

1、比较两个日期:

DateTime date1 = new DateTime(2025, 2, 26);
DateTime date2 = new DateTime(2025, 3, 1);if (date1 < date2)
{Console.WriteLine("date1 is earlier than date2");
}
else if (date1 > date2)
{Console.WriteLine("date1 is later than date2");
}
else
{Console.WriteLine("date1 is the same as date2");
}

2、判断日期范围:

DateTime start = new DateTime(2025, 2, 1);
DateTime end = new DateTime(2025, 2, 28);
DateTime testDate = new DateTime(2025, 2, 15);if (testDate >= start && testDate <= end)
{Console.WriteLine("testDate is within the range");
}

六、其他常用方法

1、判断是否为闰年:

bool isLeapYear = DateTime.IsLeapYear(2024); // 返回true

2、获取星期几:

DateTime now = DateTime.Now;
string dayOfWeek = now.DayOfWeek.ToString(); // 返回星期几(如:星期三)

七、扩展方法

C#允许通过扩展方法为  DateTime  添加自定义功能。例如,添加一个  AddWeeks  方法:

public static class DateTimeExtensions
{public static DateTime AddWeeks(this DateTime date, int weeks){return date.AddDays(weeks * 7);}
}// 使用
DateTime now = DateTime.Now;
DateTime nextMonth = now.AddWeeks(4);

总结

System.DateTime  是C#中处理日期和时间的核心结构,提供了丰富的功能,满足大多数日期和时间操作的需求。通过结合  DateTime  和  TimeSpan  ,可以轻松实现日期计算、格式化和比较等操作。


http://www.ppmy.cn/embedded/169156.html

相关文章

TCP长连接与短连接

TCP长连接与短连接 TCP&#xff08;传输控制协议&#xff09;中的长连接和短连接是两种不同的连接管理方式&#xff0c;各有优缺点&#xff1a; 短连接 短连接是指客户端与服务器完成一次数据交换后就断开连接。下次需要通信时&#xff0c;再重新建立连接。 特点&#xff1…

k8s环境搭建(从创建完一台虚拟机开始)

注意&#xff1a; 在这之前&#xff0c;所有主机需要关闭防火墙&#xff01;&#xff01;&#xff01; 1. docker 安装 所有主机都需要 1.1配置仓库 vim /etc/yum.repos.d/docker.repo https://mirror.tuna.tsinghua.edu.cn/docker-ce/linux/rhel/9/x86_64/stable/ 1.2 下…

RabbitMQ 集群部署方案

RabbitMQ 一、安装 RabbitMQ 二、更改配置文件 三、配置集群 四、测试 环境准备&#xff1a;三台服务器&#xff0c;系统是 CentOS7 IP地址分别是&#xff1a; rabbitmq1&#xff1a;192.168.152.71rabbitmq2&#xff1a;192.168.152.72rabbitmq3&#xff1a;192.168.152.…

三个小时学完vue3(一)

Vue3 之前就学过一些&#xff0c;不过用的比较少&#xff0c;基本忘完了/(ㄒoㄒ)/~~ 跟着B站视频迅速回忆一下 创建一个Vue 3 应用 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport&…

【PyCharm安装】【Python安装】【PyCharm使用】全流程

2025 - 02 - 28 - 第 64 篇 Author: 郑龙浩 / 仟濹 【PyCharm安装】【Python安装】【PyCharm使用】 文章目录 Python安装PyCharm安装及使用一 Python安装1 **Python 安装包里都包括什么呢&#xff1f;&#xff1f;&#xff1f;**2 安装时的一些选项勾选有何作用&#xff1f;(1…

【愚公系列】《Python网络爬虫从入门到精通》035-DataFrame数据分组统计整理

标题详情作者简介愚公搬代码头衔华为云特约编辑,华为云云享专家,华为开发者专家,华为产品云测专家,CSDN博客专家,CSDN商业化专家,阿里云专家博主,阿里云签约作者,腾讯云优秀博主,腾讯云内容共创官,掘金优秀博主,亚马逊技领云博主,51CTO博客专家等。近期荣誉2022年度…

ssh配置 远程控制 远程协作 github本地配置

0.设备版本 windows11 ubuntu24.0.4 1.1 在 Linux 上启用 SSH 服务 首先&#xff0c;确保 Linux 计算机上安装并启用了 SSH 服务。 安装和启动 OpenSSH 服务&#xff08;如果未安装&#xff09; # 在终端安装 OpenSSH 服务&#xff08;如果尚未安装&#xff09; sudo apt …

行为型模式 - 状态模式 (State Pattern)

行为型模式 - 状态模式 (State Pattern) 状态模式是一种行为设计模式&#xff0c;它允许对象在其内部状态改变时改变它的行为&#xff0c;对象看起来似乎修改了它的类。下面为你介绍几个状态模式的经典案例。 // 抽象状态类 abstract class LiftState {protected Lift lift;pu…