C#常用日期时间方法汇总
作者:農碼一生 时间:2023-01-04 23:44:55
一、月份英文简写
DateTime dt = DateTime.Now;
string MM = dt.AddMonths(-1).ToString("MMM", new System.Globalization.CultureInfo("en-us"));//月英文缩写:Jul
二、当月第一天和最后一天
DateTime ThisMonth_Frist = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date;
DateTime ThisMOnth_Last = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date.AddMonths(1).AddSeconds(-1);
三、上月第一天和最后一天
DateTime Today = DateTime.Today;//当天时间
DateTime ThisMonth = new DateTime(Today.Year, Today.Month, 1);//当前月第一天时间
DateTime LastMonth_First = ThisMonth.AddMonths(-1);//上月第一天时间
DateTime LastMonth_Last = ThisMonth.AddDays(-1);//上月最后一天时间
四、本周第几天
int daysInWeek1 = (int)DateTime.Now.DayOfWeek;//注意:此处周,日时返回0,
int daysInWeek2 = (int)DateTime.Now.DayOfWeek == 0 ? 7 : (int)DateTime.Now.DayOfWeek;//当前周第几天,注释:周日为0
五、本月第几周
int a = WeekOfMonth(DateTime.Now, false);//
六、本年第几周
private int WeekOfYear()
{
var dt = DateTime.Now;
int firstWeekend = Convert.ToInt32(DateTime.Parse(dt.Year + "-1-1").DayOfWeek);
int weekDay = firstWeekend == 0 ? 1 : (7 - firstWeekend + 1);
int currentDay = dt.DayOfYear;
int current_week = Convert.ToInt32(Math.Ceiling((currentDay - weekDay) / 7.0)) + 1;
return current_week;
}
七、前几周的周一和周日
private void FEDayInLastWeek()
{
int N = 3;//前几周参数
DateTime Today = DateTime.Now;
int daysInWeek = (int)Today.DayOfWeek == 0 ? 7 : (int)Today.DayOfWeek;//当前周第几天,注释:周日为0
for (int i = N; i > 0; i--)
{
//起始日期
DateTime firstDay = Today.AddDays(1 - (7 * i + daysInWeek));
DateTime lastDay = Today.AddDays(7 - (7 * i + daysInWeek));
}
}
八、本周一和当前日
private void FristDayToNowInThisWeek()
{
int daysInWeek = (int)DateTime.Now.DayOfWeek == 0 ? 7 : (int)DateTime.Now.DayOfWeek;//当前周第几天,注释:周日为0
//起始日期
DateTime firstDay = DateTime.Now.AddDays(1 - daysInWeek);
DateTime lastDay = DateTime.Now;
}
C#中控件绑定时间栏位格式化
Bind("查询出的栏位", "{0:yyyy/MM/dd}")
来源:https://www.cnblogs.com/wml-it/p/14963113.html
标签:C#,日期,时间,方法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c# 插入数据效率测试(mongodb)
2021-12-16 12:46:07
![](https://img.aspxhome.com/file/2023/2/122632_0s.png)
Spring Boot实现配置文件的自动加载和刷新功能
2023-07-14 02:26:55
![](https://img.aspxhome.com/file/2023/1/82381_0s.png)
Java 8中的18个常用日期处理(收藏)
2023-03-02 02:01:54
![](https://img.aspxhome.com/file/2023/1/66491_0s.jpg)
Intellij IDEA中如何查看maven项目中所有jar包的依赖关系图
2023-02-01 23:34:03
![](https://img.aspxhome.com/file/2023/6/80276_0s.png)
Mybatis一对多关联关系映射实现过程解析
2021-07-13 06:22:59
![](https://img.aspxhome.com/file/2023/1/66031_0s.png)
spring配置不扫描service层的原因解答
2022-03-24 16:35:02
![](https://img.aspxhome.com/file/2023/0/101940_0s.png)
Android实现客户端语音动弹界面实例代码
2023-03-04 08:27:04
![](https://img.aspxhome.com/file/2023/3/114353_0s.jpg)
SpringMVC中使用@PathVariable绑定路由中的数组的方法
2023-11-27 14:21:01
![](https://img.aspxhome.com/file/2023/9/61289_0s.png)
理解Java中的静态绑定和动态绑定
2022-05-28 04:20:06
优化SpringBoot程序启动速度的实现
2023-09-28 13:04:52
![](https://img.aspxhome.com/file/2023/7/68727_0s.jpg)
java输入数字,输出倒序的实例
2023-06-20 01:59:44
![](https://img.aspxhome.com/file/2023/9/91109_0s.jpg)
Springboot创建项目的图文教程(idea版本)
2022-09-30 13:30:17
![](https://img.aspxhome.com/file/2023/7/63157_0s.png)
Android模仿知乎的回答详情页的动画效果
2022-12-25 06:08:57
![](https://img.aspxhome.com/file/2023/6/139546_0s.gif)
C# Winform实现进度条显示
2023-09-14 15:47:51
![](https://img.aspxhome.com/file/2023/9/68609_0s.png)
C#配置文件Section节点处理总结
2022-09-21 06:29:08
![](https://img.aspxhome.com/file/2023/1/81061_0s.png)
导入项目出现Java多个工程相互引用异常A cycle was detected in the build path of project的解决办法
2023-06-26 16:27:17
![](https://img.aspxhome.com/file/2023/5/101935_0s.png)
Android百度地图应用之基本地图功能实现
2022-11-20 07:01:41
![](https://img.aspxhome.com/file/2023/2/138472_0s.jpg)
java开发RocketMQ之NameServer路由管理源码分析
2023-10-27 20:56:12
![](https://img.aspxhome.com/file/2023/6/61266_0s.png)
Android Spinner 组件的应用实例
2022-07-12 04:50:13
Android Handler,Message,MessageQueue,Loper源码解析详解
2022-04-17 17:34:57
![](https://img.aspxhome.com/file/2023/7/103867_0s.png)