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


Posted in Python onSeptember 08, 2020

前言

本文主要介绍的是关于利用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 相关文章推荐
Python文档生成工具pydoc使用介绍
Jun 02 Python
python使用MySQLdb访问mysql数据库的方法
Aug 03 Python
Python下rrdtool模块的基本使用方法
Nov 13 Python
Python实现图片尺寸缩放脚本
Mar 10 Python
python 使用matplotlib 实现从文件中读取x,y坐标的可视化方法
Jul 04 Python
Django打印出在数据库中执行的语句问题
Jul 25 Python
如何基于Python实现电子邮件的发送
Dec 16 Python
Python StringIO如何在内存中读写str
Jan 07 Python
Python使用Pyqt5实现简易浏览器(最新版本测试过)
Apr 27 Python
利用Python实现某OA系统的自动定位功能
May 27 Python
详解python使用金山词霸的翻译功能(调试工具断点的使用)
Jan 07 Python
python中__slots__节约内存的具体做法
Jul 04 Python
Python 使用Opencv实现目标检测与识别的示例代码
Sep 08 #Python
Python requests接口测试实现代码
Sep 08 #Python
Python unittest装饰器实现原理及代码
Sep 08 #Python
Python selenium环境搭建实现过程解析
Sep 08 #Python
Python unittest生成测试报告过程解析
Sep 08 #Python
Python使用Selenium模拟浏览器自动操作功能
Sep 08 #Python
Python unittest如何生成HTMLTestRunner模块
Sep 08 #Python
You might like
PHP中常用的魔术方法
2017/04/28 PHP
js兼容标准的表格变色效果
2008/06/28 Javascript
JavaScript中的Array对象使用说明
2011/01/17 Javascript
实测jquery data()如何存值
2013/08/18 Javascript
使用jQuery实现的掷色子游戏动画效果
2014/03/14 Javascript
jquery用offset()方法获得元素的xy坐标
2014/09/06 Javascript
js实现的二级横向菜单条实例
2015/08/22 Javascript
JS验证字符串功能
2017/02/22 Javascript
Angular2关于@angular/cli默认端口号配置的问题
2017/07/15 Javascript
Vue框架下引入ActiveX控件的问题解决
2019/03/25 Javascript
ES6对象操作实例详解
2020/05/23 Javascript
详谈Vue.js框架下main.js,App.vue,page/index.vue之间的区别
2020/08/12 Javascript
JavaScript获取时区实现过程解析
2020/09/24 Javascript
微信小程序实现文件预览
2020/10/22 Javascript
详细解析Python当中的数据类型和变量
2015/04/25 Python
Python中的默认参数详解
2015/06/24 Python
基于python实现微信模板消息
2015/12/21 Python
Python+树莓派+YOLO打造一款人工智能照相机
2018/01/02 Python
selenium+python自动化测试之使用webdriver操作浏览器的方法
2019/01/23 Python
Python编程快速上手——Excel表格创建乘法表案例分析
2020/02/28 Python
Python开发企业微信机器人每天定时发消息实例
2020/03/17 Python
python递归函数用法详解
2020/10/26 Python
python使用smtplib模块发送邮件
2020/12/17 Python
Python实现王者荣耀自动刷金币的完整步骤
2021/01/22 Python
html5中去掉input type date默认样式的方法
2018/09/06 HTML / CSS
雅诗兰黛美国官网:Estee Lauder美国
2016/07/21 全球购物
洛杉矶时尚女装系列:J.ING US
2019/03/17 全球购物
摩飞电器俄罗斯官方网站:Morphy Richards俄罗斯
2020/07/30 全球购物
父亲生日宴会答谢词
2014/01/10 职场文书
如何写好优秀的创业计划书
2014/01/30 职场文书
服务宗旨标语
2014/07/01 职场文书
长征观后感
2015/06/09 职场文书
反腐倡廉学习心得体会范文
2015/08/15 职场文书
导游词之安徽九华山
2019/09/18 职场文书
分享15个Webpack实用的插件!!!
2021/03/31 Javascript
Golang二维切片初始化的实现
2021/04/08 Golang