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

 更新时间:2020年09月08日 10:43:10   作者:mmmmm__yyy  
这篇文章主要给大家介绍了关于python获取本周、上周、本月、上月及本季的时间的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前言

本文主要介绍的是关于利用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获取本周、上周、本月、上月及本季的时间的文章就介绍到这了,更多相关python获取本周、上周、本月、上月及本季时间内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python使用plt库实现绘制动态曲线图并导出为GIF或MP4

    Python使用plt库实现绘制动态曲线图并导出为GIF或MP4

    这篇文章主要为大家详细介绍了Python如何使用plt库实现绘制动态曲线图并导出为GIF或MP4,文中的示例代码讲解详细,需要的可以了解一下
    2024-03-03
  • python用pandas数据加载、存储与文件格式的实例

    python用pandas数据加载、存储与文件格式的实例

    今天小编就为大家分享一篇python用pandas数据加载、存储与文件格式的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12
  • 如何基于python操作excel并获取内容

    如何基于python操作excel并获取内容

    这篇文章主要介绍了如何基于python操作excel并获取内容,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • Python OpenCV实现鼠标画框效果

    Python OpenCV实现鼠标画框效果

    这篇文章主要为大家详细介绍了Python OpenCV实现鼠标画框效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-08-08
  • SQLAlchemy的主要组件详细讲解

    SQLAlchemy的主要组件详细讲解

    SQLAlchemy是一个基于Python实现的ORM框架,能满足大多数数据库操作需求,同时支持多种数据库引擎(SQLite,MySQL,Postgresql,Oracle等),这篇文章主要介绍了SQLAlchemy的主要组件有哪些,本文给大家介绍的非常详细,对大家的学习具有一定的参考借鉴价值,需要的朋友可以参考
    2023-08-08
  • Python图像运算之图像灰度直方图对比详解

    Python图像运算之图像灰度直方图对比详解

    本篇文章将结合直方图分别对比图像灰度变换前后的变化,方便大家更清晰地理解灰度变换和阈值变换,文中的示例代码讲解详细,需要的可以参考一下
    2022-08-08
  • 一行代码挖掘文化瑰宝,Python解锁古诗文世界

    一行代码挖掘文化瑰宝,Python解锁古诗文世界

    想要从文字海洋中捕获珍珠般的古诗文吗?通过Python爬取古诗文网,你将掌握直捣黄龙的技能,简单几步,让经典文学尽收囊中,为你的项目增添灵气,需要的朋友可以参考下
    2024-03-03
  • Python异常处理机制结构实例解析

    Python异常处理机制结构实例解析

    这篇文章主要介绍了Python异常处理机制结构实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • wxpython多线程防假死与线程间传递消息实例详解

    wxpython多线程防假死与线程间传递消息实例详解

    今天小编就为大家分享一篇wxpython多线程防假死与线程间传递消息实例详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • python + winrm 实现远程连接Windows服务器并执行指定命令的操作过程

    python + winrm 实现远程连接Windows服务器并执行指定命令的操作过程

    Windows远程管理(WinRM)是Windows Server 2003 R2,Windows Vista和Windows Server 2008中一种新式的方便远程管理的服务,这篇文章主要介绍了python + winrm 实现远程连接Windows服务器并执行指定命令的操作过程,需要的朋友可以参考下
    2023-10-10

最新评论