C#中的Date Time类

DateTime类

用来进行时间操作的,类型的变量一般用于储存一个时间

DateTime.Now 属性,用于获取当前的系统时间

复制代码
DateTime dt = DateTime.Now;
Console.WriteLine(dt); //2024 / 5 / 28 10:12:27

//可以通过时间对象的属性,获取单独的某个时间详情。
Console.WriteLine($"年:{dt.Year}");
Console.WriteLine($"月:{dt.Month}");
Console.WriteLine($"日:{dt.Day}");
Console.WriteLine($"时:{dt.Hour}");
Console.WriteLine($"分:{dt.Minute}");
Console.WriteLine($"秒:{dt.Second}");
Console.WriteLine($"毫秒:{dt.Millisecond}");

Console.WriteLine($"今天是周几,{dt.DayOfWeek}"); //Tuesday
Console.WriteLine($"今天是周几,{(int)dt.DayOfWeek}"); //转换为数字。2 ,0-6(0代表周日,其余正常)
Console.WriteLine($"今天是周几,{dt.DayOfYear}"); //147, 获取当前是一年中的第几天


// 把当前的时间转换为字符串类型,可以有不同的格式
Console.WriteLine(dt.ToString());//2024/5/28 10:26:30
Console.WriteLine(dt.ToString("G"));//2024/5/28 10:26:30
Console.WriteLine(dt.ToString("s"));//2024-05-28T10:28:32
Console.WriteLine(dt.ToString("d"));//2024/5/28
Console.WriteLine(dt.ToString("D"));//2024年5月28日
Console.WriteLine(dt.ToString("F"));//2024年5月28日 10:29:16


//格式化的时候,一些字符可以代表时间属性
Console.WriteLine(dt.ToString("yy")); //24年的后两位
Console.WriteLine(dt.ToString("M"));
Console.WriteLine(dt.ToString("dd"));
Console.WriteLine(dt.ToString("m"));
Console.WriteLine(dt.ToString("s"));
Console.WriteLine(dt.ToString("fff"));
Console.WriteLine(dt.ToString("现在是小时是:H")); //注意:如果单独使用H/h报错,拼接字符串

Console.WriteLine(dt.ToString("今年是yyyy年MM月dd日HH时mm分ss秒,今天是dddd"));

Console.WriteLine("========================");

// 时间戳,把当前时间转换为秒 / 毫秒
DateTime time = DateTime.Now;
Console.WriteLine(new DateTimeOffset(time));//换算为格林威治时间
Console.WriteLine(new DateTimeOffset(time).ToUnixTimeSeconds()); //秒戳
Console.WriteLine(new DateTimeOffset(time).ToUnixTimeMilliseconds()); //毫秒戳

//生成目标时间对象
DateTime myTime = new DateTime(2003, 3, 3, 11, 20, 58);
Console.WriteLine(myTime.ToString());

清除上面代码

Console.Clear();

Console.WriteLine(DateTime.Now.ToString("今年是yyyy年MM月dd日HH时mm分ss秒,今天是dddd"));

让代码停止1秒,单位毫秒

Thread.Sleep(1000);

时间的计算和对比

复制代码
//时间的操作
DateTime dateTime = DateTime.Now;
Console.WriteLine(dateTime);
//可以通过调整时间对象的ADDXXX()方法,来修改当前的时间对象的年月日时分秒。
dateTime = dateTime.AddYears(1);
dateTime = dateTime.AddMonths(1);
Console.WriteLine(dateTime.ToString());
dateTime = dateTime.AddYears(-5);
Console.WriteLine(dateTime.ToString());

//上面的修改,不会对真正的时间对象进行修改。
DateTime dateTime1 = DateTime.Now;
Console.WriteLine(dateTime1);


// 如果需要进行时间比较,可以使用 > < >= <= == != 运算符。
if (DateTime.Now < new DateTime(2028, 1, 1))
{
    Console.WriteLine("ok");
}
else
{
    Console.WriteLine("NO");
}

// 可以直接使用-进行时间的计算。 TimeSpan得到时间的跨度。
TimeSpan tm = new DateTime(2026,1,1) - DateTime.Now;
Console.WriteLine($"到2026年1月1日还有{tm.Days}天");
Console.WriteLine($"到2026年1月1日还有{tm.TotalHours}小时");
相关推荐
光电笑映1 小时前
TCP 为什么可靠?从三次握手、滑动窗口到拥塞控制全面拆解(上)
运维·服务器·网络·udp
飞奔的瓜牛2 小时前
Linux 下使用 OpenSSL 生成自签名证书完整指南
运维·服务器·nginx
codeejun2 小时前
每日一Go·MySQL-17、分页优化方案
数据库·mysql
CoderYanger2 小时前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第四弹):图书管理系统(用户登录+添加图书)
java·数据库·spring boot·mysql·面试·java-ee·mybatis
Decay__3 小时前
附 - 02・SQLServer 分库分表与中间件 —— 原生分区表、横向分库实践与联邦查询
运维·数据库·sql·mysql·架构
牧瀬クリスだ3 小时前
从零手写博客系统:分层架构与统一响应(一)
java·数据库·架构·mybatis·springboot·安全架构
the局外人3 小时前
Python 实战检索优化:BM25、向量与重排
数据库·后端·python
得物技术3 小时前
从研发经验到团队知识:Akashic知识工程实践|得物技术
数据库·程序员·架构
ClouGence3 小时前
开源数据库管理工具 CloudDM 发布新版本,支持 GreptimeDB 数据源
数据库·kafka
BruceNeter4 小时前
C# winform卡死问题分析1——线程池子线程创建句柄
c#·.net