C#获取上个月第一天和最后一天日期的方法
更新时间:2014年11月14日 14:51:38 投稿:shichen2014
这篇文章主要介绍了C#获取上个月第一天和最后一天日期的方法,是关于C#日期函数的简单应用,具有一定的实用价值,需要的朋友可以参考下
本文实例讲述了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) + "日";//上个月最后一天
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#常见的几种集合 ArrayList,Hashtable,List<T>,Dictionary<K,
本文对C#中常见集合ArrayList,Hashtable,List<T>,Dictionary<K,V>遍历方法做了简单的对比和介绍,有需要的朋友可以参考一下。2016-03-03


最新评论