C#获取上个月第一天和最后一天日期的方法
作者:shichen2014 时间:2023-02-22 07:33:44
本文实例讲述了C#获取上个月第一天和最后一天日期的方法。分享给大家供大家参考。
具体实现代码如下:
int year = DateTime.Now.Year;//当前年
int mouth = DateTime.Now.Month;//当前月
int beforeYear = 0;
int beforeMouth = 0;
if (mouth <= 1)//如果当前月是一月,那么年份就要减1
{
beforeYear = year - 1;
beforeMouth =12;//上个月
}
else
{
beforeYear = year;
beforeMouth = mouth - 1;//上个月
}
string beforeMouthOneDay = beforeYear + "年" + beforeMouth + "月" + 1 + "日";//上个月第一天
string beforeMouthLastDay = beforeYear + "年" + beforeMouth + "月" + DateTime.DaysInMonth(year, beforeMouth) + "日";//上个月最后一天
上个月最后一天也可以这样写:
string beforeMouthLastDay = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-1).ToString("yyyy-MM-dd"); //获取上个月最后一天日期
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,获取,日期
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
dialog dismiss时键盘不消失的问题浅析及解决办法
2021-09-18 10:06:30
![](https://img.aspxhome.com/file/2023/0/120060_0s.gif)
C#、ASP.NET通用扩展工具类之LogicSugar
2023-11-18 09:56:07
SpringBoot导入Druid运行失败问题
2023-02-14 03:35:41
![](https://img.aspxhome.com/file/2023/0/108950_0s.jpg)
Android中Button实现点击换图案及颜色
2022-06-28 00:42:10
![](https://img.aspxhome.com/file/2023/8/120128_0s.jpg)
Java中try、catch的使用方法
2021-11-08 00:48:12
![](https://img.aspxhome.com/file/2023/6/77046_0s.png)
Spring @Conditional注解原理解析
2022-10-04 16:09:51
android调试工具DDMS的使用详解
2023-06-21 09:06:22
![](https://img.aspxhome.com/file/2023/4/82414_0s.png)
SpringBoot使用Spring Security实现登录注销功能
2023-01-05 13:19:54
![](https://img.aspxhome.com/file/2023/8/92538_0s.png)
详解使用Maven开发Web应用详细步骤
2023-08-06 14:18:20
![](https://img.aspxhome.com/file/2023/6/58026_0s.png)
解决Springboot @Autowired 无法注入问题
2022-04-14 21:19:01
Android事件分发机制全面解析
2023-05-11 06:53:49
详解MyBatis日志如何做到兼容所有常用的日志框架
2022-05-28 08:24:17
![](https://img.aspxhome.com/file/2023/4/78044_0s.png)
maven如何动态统一修改版本号的方法步骤
2022-06-05 20:08:43
![](https://img.aspxhome.com/file/2023/1/124711_0s.png)
使用JMX监控Zookeeper状态Java API
2023-05-14 02:27:26
C# 多窗口委托通信的实现
2023-01-22 02:21:31
![](https://img.aspxhome.com/file/2023/6/104256_0s.png)
教你用JAVA写文本编辑器(一)
2023-06-13 09:05:31
Java中关于二叉树层序遍历深入了解
2023-07-26 07:06:09
![](https://img.aspxhome.com/file/2023/6/61076_0s.png)
IntelliJ IDEA快速创建getter和setter方法
2023-06-04 00:44:46
![](https://img.aspxhome.com/file/2023/0/61650_0s.jpg)
Kotlin协程launch原理详解
2023-05-19 09:36:01
基于C#实现获取本地磁盘目录
2021-08-21 12:51:43
![](https://img.aspxhome.com/file/2023/3/129753_0s.png)