前言
本文主要介绍的是关于利用python 获取本周,上周,本月,上月,本季的时间,话不多说了,来一起看看实现的方法吧
示例代码:
import datetime from datetime import timedelta now = datetime.datetime.now() # 今天 today = now print('--- today = {}'.format(today)) # 昨天 yesterday = now - timedelta(days=1) print('--- yesterday = {}'.format(yesterday)) # 明天 tomorrow = now + timedelta(days=1) print('--- tomorrow = {}'.format(tomorrow)) # 当前季度 now_quarter = now.month / 3 if now.month % 3 == 0 else now.month / 3 + 1 print('--- now_quarter = {}'.format(now_quarter)) # 本周第一天和最后一天 this_week_start = now - timedelta(days=now.weekday()) this_week_end = now + timedelta(days=6 - now.weekday()) print('--- this_week_start = {} this_week_end = {}'.format(this_week_start, this_week_end)) # 上周第一天和最后一天 last_week_start = now - timedelta(days=now.weekday() + 7) last_week_end = now - timedelta(days=now.weekday() + 1) print('--- last_week_start = {} last_week_end = {}'.format(last_week_start, last_week_end)) # 本月第一天和最后一天 this_month_start = datetime.datetime(now.year, now.month, 1) this_month_end = datetime.datetime(now.year, now.month + 1, 1) - timedelta(days=1)+ datetime.timedelta( hours=23, minutes=59, seconds=59) print('--- this_month_start = {} this_month_end = {}'.format(this_month_start, this_month_end)) # 上月第一天和最后一天 last_month_end = this_month_start - timedelta(days=1)+ datetime.timedelta( hours=23, minutes=59, seconds=59) last_month_start = datetime.datetime(last_month_end.year, last_month_end.month, 1) print('--- last_month_end = {} last_month_start = {}'.format(last_month_end, last_month_start)) # 本季第一天和最后一天 month = (now.month - 1) - (now.month - 1) % 3 + 1 this_quarter_start = datetime.datetime(now.year, month, 1) this_quarter_end = datetime.datetime(now.year, month + 3, 1) - timedelta(days=1)+ datetime.timedelta( hours=23, minutes=59, seconds=59) print('--- this_quarter_start = {} this_quarter_end = {}'.format(this_quarter_start, this_quarter_end)) # 上季第一天和最后一天 last_quarter_end = this_quarter_start - timedelta(days=1)+ datetime.timedelta( hours=23, minutes=59, seconds=59) last_quarter_start = datetime.datetime(last_quarter_end.year, last_quarter_end.month - 2, 1) print('--- last_quarter_start = {} last_quarter_end = {}'.format(last_quarter_start, last_quarter_end)) # 本年第一天和最后一天 this_year_start = datetime.datetime(now.year, 1, 1) this_year_end = datetime.datetime(now.year + 1, 1, 1) - timedelta(days=1)+ datetime.timedelta( hours=23, minutes=59, seconds=59) print('--- this_year_start = {} this_year_end = {}'.format(this_year_start, this_year_end)) # 去年第一天和最后一天 last_year_end = this_year_start - timedelta(days=1)+ datetime.timedelta( hours=23, minutes=59, seconds=59) last_year_start = datetime.datetime(last_year_end.year, 1, 1) print('--- last_year_start = {} last_year_end = {}'.format(last_year_start, last_year_end))
总结
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“python获取本周、上周、本月、上月及本季的时间代码实例”评论...
更新日志
2024年11月08日
2024年11月08日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]