• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python 日期区间处理 (本周本月上周上月…)

python 搞代码 4年前 (2022-01-07) 42次浏览 已收录 0个评论

这篇文章主要介绍了Python 日期区间处理 (本周本月上周上月…),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

工具类

 class CalendarUtils: """ 日期工具类 """ @staticmethod def delta_day(delta=0): """ :param delta:  偏移量 :return:    0今天, 1昨天, 2前天, -1明天 ... """ return (datetime.now() + timedelta(days=delta)).strftime('%Y-%m-%d') @staticmethod def delta_week(delta=0): """ :param delta:  偏移量 :return:    0本周, -1上周, 1下周 ... """ now = datetime.now() week = now.weekday() _from = (now - timedelta(days=week - 7 * delta)).strftime('%Y-%m-%d') _to = (now + timedelta(days=6 - week + 7 * delta)).strftime('%Y-%m-%d') return _from, _to @staticmethod def delta_month(delta=0): """ :param delta:  偏移量 :return:    0本月, -1上月, 1下月, 下下个月... """ def _delta_month(__year, __month, __delta): _month = __month + __delta if _month  12: delta_year = math.floor(_month / 12) __year += delta_year _month %= 12 return __year, _month now = datetime.now() _from = datetime(*_delta_month(now.year, now.month, delta), 1) _to = datetime(*_delta_month(_from.year, _from.month, 1), 1) - timedelta(days=1) return _from.strftime('%Y-%m-%d'), _to.strftime('%Y-%m-%d') @staticmethod def delta_year(delta=0): """ :param delta:  偏移量 :return:    0今年, -1去年, 1明年 ... """ now = datetime.now() _from = datetime(now.year + delta, 1, 1) _to = datetime(_from.year + 1, 1, 1) - timedelta(days=1) return _from.strftime('%Y-%m-%d'), _to.strftime('%Y-%m-%d') if __name__ == '__main__': print('当前日期: ', datetime.now()) print('*' * 40) print('今天: ', CalendarUtils.delta_day()) print('昨天: ', CalendarUtils.delta_day(-1)) print('前天: ', CalendarUtils.delta_day(-2)) print('明天: ', CalendarUtils.delta_day(1)) print('后天: ', CalendarUtils.delta_day(2)) print('*' * 40) print('本周: ', CalendarUtils.delta_week()) print('上周: ', CalendarUtils.delta_week(-1)) print('下周: ', CalendarUtils.delta_week(1)) print('*' * 40) print('本月: ', CalendarUtils.delta_month()) print('上月: ', CalendarUtils.delta_month(-1)) print('下月: ', CalendarUtils.delta_month(1)) print('*' * 40) print('本年: ', CalendarUtils.delta_year()) print('去年: ', CalendarUtils.delta_year(-1)) print('明年: ', CalendarUtils.delta_year(1))

运行结果

当前日期:  2019-06-26 11:01:34.662560
****************************************
今天:  2019-06-26
昨天:  2019-06-25
前天:  2019-06-24
明天:  2019-06-27
后天:  2019-06-28
****************************************
本周:  (‘2019-06-24’, ‘2019-06-30’)
上周:  (‘2019-06-17’, ‘2019-06-23’)
下周:  (‘2019-07-01’, ‘2019-07-07’)
****************************************
本月:  (‘2019-06-01’, ‘2019-06-30’)
上月:  (‘2019-05-01’, ‘2019-05-31’)
下月:  (‘2019-07-01’, ‘2019-07-31’)
****************************************
本年:  (‘2019-01-01’, ‘2019-12-31’)
去年:  (‘2018-01-01’, ‘2018来源[email protected]搞@^&代*@码)网-12-31′)
明年:  (‘2020-01-01’, ‘2020-12-31’)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是Python 日期区间处理 (本周本月上周上月…)的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python 日期区间处理 (本周本月上周上月…)
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址