python获取本周、上周、本月、上月及本季的时间代码实例

所属分类: 脚本专栏 / python 阅读数: 1761
收藏 0 赞 0 分享

前言

本文主要介绍的是关于利用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))

总结

更多精彩内容其他人还在看

Python简单操作sqlite3的方法示例

这篇文章主要介绍了Python简单操作sqlite3的方法,结合实例形式分析了Python针对sqlite3数据库的读取、创建、增删改查等基本操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Python实现遍历目录的方法【测试可用】

这篇文章主要介绍了Python实现遍历目录的方法,涉及Python针对目录与文件的遍历、判断、读取相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

python条件变量之生产者与消费者操作实例分析

这篇文章主要介绍了python条件变量之生产者与消费者操作,结合具体实例形式分析了Python条件变量的概念、原理、及线程操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

利用pyinstaller或virtualenv将python程序打包详解

这篇文章主要给大家介绍了利用pyinstaller将python程序打包的相关资料,文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

Python多线程经典问题之乘客做公交车算法实例

这篇文章主要介绍了Python多线程经典问题之乘客做公交车算法,简单描述了乘客坐公交车问题并结合实例形式分析了Python多线程实现乘客坐公交车算法的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Python编程实现删除VC临时文件及Debug目录的方法

这篇文章主要介绍了Python编程实现删除VC临时文件及Debug目录的方法,涉及Python针对文件与目录的遍历、删除等相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

python3中dict(字典)的使用方法示例

这篇文章主要介绍了python3中dict(字典)的使用方法,文中给出了详细的功能列举,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

Python中.py文件打包成exe可执行文件详解

这篇文章主要给大家介绍了在Python中.py文件打包成exe可执行文件的相关资料,文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

Python编程之event对象的用法实例分析

这篇文章主要介绍了Python编程之event对象的用法,结合实例形式分析了event对象在线程通信中的作用与使用方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Python爬取网页中的图片(搜狗图片)详解

没想到python是如此强大,令人着迷,以前看见图片总是一张一张复制粘贴,现在好了,学会python就可以用程序将一张张图片,保存下来。下面这篇文章主要给大家介绍了利用Python3.6爬取搜狗图片网页中图片的相关资料,需要的朋友可以参考下。
收藏 0 赞 0 分享
查看更多