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 相关文章推荐
剖析Django中模版标签的解析与参数传递
Jul 21 Python
Python实现的个人所得税计算器示例
Jun 01 Python
python调用百度语音识别实现大音频文件语音识别功能
Aug 30 Python
python爬虫刷访问量 2019 7月
Aug 01 Python
python 画出使用分类器得到的决策边界
Aug 21 Python
Python对wav文件的重采样实例
Feb 25 Python
PyCharm中Matplotlib绘图不能显示UI效果的问题解决
Mar 12 Python
浅谈Python中的字符串
Jun 10 Python
Django Admin后台模型列表页面如何添加自定义操作按钮
Nov 11 Python
python-图片流传输的思路及示例(url转换二维码)
Dec 21 Python
python数字图像处理之图像的批量处理
Jun 28 Python
Django中celery的使用项目实例
Jul 07 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数组是否为空的代码
2011/09/08 PHP
CodeIgniter中使用Smarty3基本配置
2015/06/29 PHP
django中的ajax组件教程详解
2018/10/18 PHP
HR vs ForZe BO3 第二场 2.13
2021/03/10 DOTA
Javascript 兼容firefox的一些问题
2009/05/21 Javascript
Domino中运用jQuery读取视图内容的方法
2009/10/21 Javascript
js parsefloat parseint 转换函数
2010/01/21 Javascript
JS在一定时间内跳转页面及各种刷新页面的实现方法
2016/05/26 Javascript
需灵活掌握的Bootstrap预定义排版类 你精通吗?
2016/06/20 Javascript
js实现非常棒的弹出div
2016/10/06 Javascript
localStorage的黑科技-js和css缓存机制
2017/02/06 Javascript
Vue的Flux框架之Vuex状态管理器
2017/07/30 Javascript
JS排序算法之希尔排序与快速排序实现方法
2017/12/12 Javascript
Bootstrap-table使用footerFormatter做统计列功能
2018/09/07 Javascript
vue调试工具vue-devtools安装及使用方法
2018/11/07 Javascript
vue获取form表单的值示例
2019/10/29 Javascript
vue设置默认首页的操作
2020/08/12 Javascript
原生js中运算符及流程控制示例详解
2021/01/05 Javascript
Python开发的HTTP库requests详解
2017/08/29 Python
Python网络编程详解
2017/10/31 Python
tensorflow实现逻辑回归模型
2018/09/08 Python
python中pika模块问题的深入探究
2018/10/13 Python
Python定时发送消息的脚本:每天跟你女朋友说晚安
2018/10/21 Python
在Python中如何传递任意数量的实参的示例代码
2019/03/21 Python
连接pandas以及数组转pandas的方法
2019/06/28 Python
python使用pandas抽样训练数据中某个类别实例
2020/02/28 Python
JD Sports丹麦:英国领先的运动时尚零售商
2020/11/24 全球购物
师德建设实施方案
2014/03/21 职场文书
服装设计专业自荐信
2014/06/17 职场文书
税务职业生涯规划书范文
2014/09/16 职场文书
教师学期末个人总结
2015/02/13 职场文书
基层工作经历证明
2015/06/19 职场文书
教学反思怎么写
2016/02/24 职场文书
2019年鼓励无偿献血倡议书
2019/09/17 职场文书
Python字典的基础操作
2021/11/01 Python
Nginx 反向代理解决跨域问题多种情况分析
2022/01/18 Servers