C# 常用日期时间函数(老用不熟)
时间:2021-08-21 10:12:18
--DateTime 数字型
System.DateTime currentTime=new System.DateTime();
1.1 取当前年月日时分秒
currentTime=System.DateTime.Now;
1.2 取当前年
int 年=currentTime.Year;
1.3 取当前月
int 月=currentTime.Month;
1.4 取当前日
int 日=currentTime.Day;
1.5 取当前时
int 时=currentTime.Hour;
1.6 取当前分
int 分=currentTime.Minute;
1.7 取当前秒
int 秒=currentTime.Second;
1.8 取当前毫秒
int 毫秒=currentTime.Millisecond;
(变量可用中文)
1.9 取中文日期显示——年月日时分
string strY=currentTime.ToString("f"); //不显示秒
1.10 取中文日期显示_年月
string strYM=currentTime.ToString("y");
1.11 取中文日期显示_月日
string strMD=currentTime.ToString("m");
1.12 取当前年月日,格式为:2003-9-23
string strYMD=currentTime.ToString("d");
1.13 取当前时分,格式为:14:24
string strT=currentTime.ToString("t");
//今天
DateTime.Now.Date.ToShortDateString();
//昨天,就是今天的日期减一
DateTime.Now.AddDays(-1).ToShortDateString();
//明天,同理,加一
DateTime.Now.AddDays(1).ToShortDateString();
//本周(要知道本周的第一天就得先知道今天是星期几,从而得知本周的第一天就是几天前的那一天,要注意的是这里的每一周是从周日始至周六止
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
//如果你还不明白,再看一下中文显示星期几的方法就应该懂了
//由于DayOfWeek返回的是数字的星期几,我们要把它转换成汉字方便我们阅读,有些人可能会用switch来一个一个地对照,其实不用那么麻烦的 string[] Day = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
Day[Convert.ToInt16(DateTime.Now.DayOfWeek)];
//上周,同理,一个周是7天,上周就是本周再减去7天,下周也是一样
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
//下周
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
//本月,很多人都会说本月的第一天嘛肯定是1号,最后一天就是下个月一号再减一天。当然这是对的
//一般的写法
DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1"; //第一天
DateTime.Parse(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1").AddMonths(1).AddDays(-1).ToShortDateString();//最后一天
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Java如何实现在PDF中插入,替换或删除图像
![](https://img.aspxhome.com/file/2023/4/80864_0s.jpg)
如何解决Java多线程死锁问题
![](https://img.aspxhome.com/file/2023/0/84630_0s.png)
C/C++与Java各数据类型所占字节数的详细比较
Java字节与字符流永久存储json数据
Spring多个数据源配置详解
![](https://img.aspxhome.com/file/2023/6/79936_0s.png)
关于@Autowired注入依赖失败的问题及解决
![](https://img.aspxhome.com/file/2023/4/60474_0s.png)
Spring Cloud Gateway 远程代码执行漏洞(CVE-2022-22947)的过程解析
![](https://img.aspxhome.com/file/2023/1/72651_0s.jpg)
Flutter开发Widgets 之 PageView使用示例
![](https://img.aspxhome.com/file/2023/1/120091_0s.jpg)
IDEA Error:java:无效的源发行版:13的解决过程
![](https://img.aspxhome.com/file/2023/8/59578_0s.png)
java实现简单单链表
![](https://img.aspxhome.com/file/2023/2/58722_0s.jpg)
使用jenkins部署springboot项目的方法步骤
![](https://img.aspxhome.com/file/2023/0/62760_0s.png)
SpringBoot后端进行数据校验JSR303的使用详解
![](https://img.aspxhome.com/file/2023/5/72375_0s.png)
关于Flyweight模式应用实践的相关介绍
C# 位运算符整理
操作xml,将xml数据显示到treeview的C#代码
![](https://img.aspxhome.com/file/2023/7/85697_0s.jpg)
学会使用C#异常
![](https://img.aspxhome.com/file/2023/2/78192_0s.png)
Spring超详细讲解IOC与解耦合
![](https://img.aspxhome.com/file/2023/3/66713_0s.png)
详解tryAcquire()、addWaiter()、acquireQueued()
专属于程序员的浪漫-Java输出动态闪图iloveyou
![](https://img.aspxhome.com/file/2023/8/84608_0s.png)