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中使用CasperJS获取JS渲染生成的HTML内容的教程
Apr 09 Python
python使用fileinput模块实现逐行读取文件的方法
Apr 29 Python
浅谈使用Python变量时要避免的3个错误
Oct 30 Python
python输入错误密码用户锁定实现方法
Nov 27 Python
python并发编程之线程实例解析
Dec 27 Python
python中set()函数简介及实例解析
Jan 09 Python
TensorFlow实现MLP多层感知机模型
Mar 09 Python
python3实现elasticsearch批量更新数据
Dec 03 Python
Python&&GDAL实现NDVI的计算方式
Jan 09 Python
Django 解决开发自定义抛出异常的问题
May 21 Python
Python3爬虫中Selenium的用法详解
Jul 10 Python
浅谈Python 钉钉报警必备知识系统讲解
Aug 17 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小技巧之函数重载
2014/06/02 PHP
PHP实现打包下载文件的方法示例
2017/10/07 PHP
特殊字符、常规符号及其代码对照表
2006/06/26 Javascript
JQuery 绑定事件时传递参数的实现方法
2009/10/13 Javascript
javascript中的原型链深入理解
2014/02/24 Javascript
jquery实现点击页面计算点击次数
2015/01/23 Javascript
基于jQuery实现交互体验社会化分享代码附源码下载
2016/01/04 Javascript
浅谈$('div a') 与$('div>a')的区别
2016/07/18 Javascript
KnockoutJS 3.X API 第四章之数据控制流component绑定
2016/10/10 Javascript
vue项目中api接口管理总结
2018/04/20 Javascript
JS实现table表格内针对某列内容进行即时搜索筛选功能
2018/05/11 Javascript
详解创建自定义的Angular Schematics
2018/06/06 Javascript
Vue实现美团app的影院推荐选座功能【推荐】
2018/08/29 Javascript
Vue-CLI项目中路由传参的方式详解
2019/09/01 Javascript
js实现验证码功能
2020/07/24 Javascript
原生JS实现音乐播放器
2021/01/26 Javascript
Python3中多线程编程的队列运作示例
2015/04/16 Python
Python 迭代器工具包【推荐】
2016/05/06 Python
python3新特性函数注释Function Annotations用法分析
2016/07/28 Python
使用Python的turtle模块画图的方法
2017/11/15 Python
python XlsxWriter模块创建aexcel表格的实例讲解
2018/05/03 Python
numpy concatenate数组拼接方法示例介绍
2019/05/27 Python
浅析Windows 嵌入python解释器的过程
2019/07/26 Python
python使用requests.session模拟登录
2019/08/09 Python
python 扩展print打印文件路径和当前时间信息的实例代码
2019/10/11 Python
python使用协程实现并发操作的方法详解
2019/12/27 Python
PyTorch中反卷积的用法详解
2019/12/30 Python
基于Python数据分析之pandas统计分析
2020/03/03 Python
Python绘图之二维图与三维图详解
2020/08/04 Python
一款利用纯css3实现的win8加载动画的实例分析
2014/12/11 HTML / CSS
竞选学习委员演讲稿
2014/04/28 职场文书
人事任命书怎么写
2014/06/05 职场文书
制冷与空调专业毕业生推荐信
2014/07/07 职场文书
2014年文秘工作总结
2014/11/25 职场文书
社区六一儿童节活动总结
2015/02/11 职场文书
MySQL详细讲解变量variables的用法
2022/06/21 MySQL