计算本周、近两周、本月、近两个月的开始和结束时间


#region 日期信息类
public class DateInfo
{
private DateTime beginDate;

public DateTime BeginDate
{
get { return beginDate; }
set { beginDate = value; }
}
private DateTime endDate;

public DateTime EndDate
{
get { return endDate; }
set { endDate = value; }
}

}
#endregion
#region 获取本周的时间开始时间和结束时间 add by shirlly 20091218
/// <summary>
/// 获取本周的时间开始时间和结束时间
/// </summary>
/// <param name="curentDateTime"></param>
/// <returns></returns>
public static DateInfo getOneWeekDateInfo(DateTime curentDateTime)
{
int year = curentDateTime.Year;
int month = curentDateTime.Month;
int day = curentDateTime.Day;
int weekNum = Week(year, month, day);
DateInfo weekInfo = new DateInfo();
weekInfo.BeginDate = curentDateTime.AddDays(-weekNum + 1);
weekInfo.EndDate = curentDateTime.AddDays(7 - weekNum);
return weekInfo;
}
#endregion

#region 获取近两周的时间开始时间和结束时间 add by shirlly 20091218
/// <summary>
/// 获取近两周的时间开始时间和结束时间
/// </summary>
/// <param name="curentDateTime"></param>
/// <returns></returns>
public static DateInfo getTwoWeekDateInfo(DateTime curentDateTime)
{
int year = curentDateTime.Year;
int month = curentDateTime.Month;
int day = curentDateTime.Day;
int weekNum = Week(year, month, day);
DateInfo weekInfo = new DateInfo();
weekInfo.BeginDate = curentDateTime.AddDays(-weekNum + 1 - 7);
weekInfo.EndDate = curentDateTime.AddDays(7 - weekNum);
return weekInfo;
}
#endregion

#region 计算本月的开始时间和结束时间 add by shirlly 20091218
public static DateInfo getOneMonthDateInfo(DateTime curentDateTime)
{
int year = curentDateTime.Year;
int month = curentDateTime.Month;
int day = curentDateTime.Day;
int days = DateTime.DaysInMonth(year, month);//计算某年某月的天数
DateInfo weekInfo = new DateInfo();
weekInfo.BeginDate = curentDateTime.AddDays(-day + 1);
weekInfo.EndDate = curentDateTime.AddDays(days - day);
return weekInfo;
}
#endregion

#region 计算近两个月的开始时间和结束时间 add by shirlly 20091218
public static DateInfo getTwoMonthDateInfo(DateTime curentDateTime)
{
int year = curentDateTime.Year;
int month = curentDateTime.Month;
int day = curentDateTime.Day;
int thisMonthDays = DateTime.DaysInMonth(year, month);//计算某年某月的天数
if(month - 1<0)//如果是1月 add by shirlly 20100107
{
year = year-1;
month = 12;
}
int lastMonthDays = DateTime.DaysInMonth(year, month);
DateInfo weekInfo = new DateInfo();
weekInfo.BeginDate = curentDateTime.AddDays((-day + 1) - lastMonthDays);
weekInfo.EndDate = curentDateTime.AddDays(thisMonthDays - day);
return weekInfo;
}
#endregion
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用datetime模块来生成时间数据,并结合列表推导式来生成列表。具体实现如下: ```python import datetime # 获取当前时间 now = datetime.datetime.now() # 生成时间列表 time_list = [ [now.strftime('%Y-%m-%d 00:00:00'), now.strftime('%Y-%m-%d 23:59:59')], # 今天 [(now - datetime.timedelta(days=7)).strftime('%Y-%m-%d 00:00:00'), (now - datetime.timedelta(days=1)).strftime('%Y-%m-%d 23:59:59')], # 前一周 [(now - datetime.timedelta(days=14)).strftime('%Y-%m-%d 00:00:00'), (now - datetime.timedelta(days=8)).strftime('%Y-%m-%d 23:59:59')], # 前两周 [(now - datetime.timedelta(days=21)).strftime('%Y-%m-%d 00:00:00'), (now - datetime.timedelta(days=15)).strftime('%Y-%m-%d 23:59:59')], # 前三周 [(now - datetime.timedelta(days=28)).strftime('%Y-%m-%d 00:00:00'), (now - datetime.timedelta(days=22)).strftime('%Y-%m-%d 23:59:59')], # 前四周 ] # 打印时间列表 print(time_list) ``` 输出结果如下: ``` [ ['2022-01-06 00:00:00', '2022-01-06 23:59:59'], ['2021-12-30 00:00:00', '2022-01-05 23:59:59'], ['2021-12-23 00:00:00', '2021-12-29 23:59:59'], ['2021-12-16 00:00:00', '2021-12-22 23:59:59'], ['2021-12-09 00:00:00', '2021-12-15 23:59:59'] ] ``` 说明: - 第一行代码获取当前时间; - 第二行到第六行使用datetime模块的timedelta函数来计算时间差,得到前一周、前两周、前三周、前四周的开始时间结束时间; - 第七行到第十二行使用列表推导式来生成一个列表,其中每个元素为一个长度为2的列表,表示开始时间结束时间; - 最后一行打印出生成的时间列表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值