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}小时");
相关推荐
2501_9307077810 小时前
如何使用C#代码将 Excel 文件转换为 SVG
开发语言·c#·excel
路由侠内网穿透.10 小时前
本地部署远程服务管理软件 IntelliSSH 并实现外部访问
运维·服务器·网络·网络协议
同聘云10 小时前
阿里云国际站服务器防火墙怎么关闭?防火墙部署方式有哪些?
服务器·阿里云·云计算
同聘云10 小时前
阿里云国际站服务器独立ip有什么好处?独立ip怎么搭建?
服务器·安全·阿里云·云计算
帅那个帅10 小时前
Kubectl 命令使用总结
运维·服务器·容器
钟智强10 小时前
红队实战复盘:如何运用【火尖枪】高效突破复杂登录防线
服务器·安全·web安全·http·go·php·bruteforce
云老大TG:@yunlaoda36010 小时前
华为云国际站代理商GSL的跨境区域政策适配有哪些具体措施?
数据库·人工智能·华为云
福尔摩斯张10 小时前
嵌入式硬件篇:常见单片机型号深度解析与技术选型指南
网络·数据库·stm32·单片机·网络协议·tcp/ip·mongodb
NineData10 小时前
如何通过 NineData 将 Oracle 不停机迁移到 GaussDB
数据库·oracle·gaussdb·数据库管理工具·ninedata·数据库迁移·迁移工具
h79971010 小时前
mysql 查询语句解析笔记(按执行顺序理解)
数据库·笔记·mysql