Python中的时间函数datetime.timedelta()
Python中的时间函数
- 时间上的加减
时间上的加减
getday() 返回在某年某月某日的基础上加n天后的年月日
import datetime
import json
import random
import time
import stringdef getday(y=2017, m=8, d=15, n=1):the_date = datetime.datetime(y,m,d)result_date = the_date + datetime.timedelta(days=n) #加n天后的日期d = result_date.strftime('%Y%m%d') #格式化成字符串形式year = int(d[:4])month = int(d[4:6])day = int(d[6:8])return d,(year,month,day)d, _ = getday(2022,10,11, 20)
获取n小时后的时间
def get_later_time(cur_time, n):''':param cur_time: 当前时间 如2022040810 年月日时:param n: 单位为 h, 获取n小时后的时间:return:'''the_date = datetime.datetime.strptime(cur_time, '%Y%m%d%H')result_date = the_date + datetime.timedelta(hour=n)d = result_date.strftime('%Y%m%d%H')return d
获取hours小时,minutes分钟后的时间。
the_date = datetime.datetime.strptime(cur_time, '%Y%m%d%H%M%S')
print(the_date)
##datetime.timedelta() 进行时间上的加减,参数可以是(days,hours,minutes,seconds)等
result_date = the_date + datetime.timedelta(hours=2,minutes =20)
print(result_date)
函数说明:class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
获取 d 天 h小时 m分钟 s秒之前的时间
def get_before_time(cur_time):''':param cur_time: 当前时间 如20221012013130 年月日时分秒:param duration::return: h小时 m分钟 s秒之前的时间'''d =random.randint(0,1)h = random.randint(0, 23)m = random.randint(0, 59)s = random.randint(0, 59)the_date = datetime.datetime.strptime(cur_time, '%Y%m%d%H%M%S')result_date = the_date - datetime.timedelta(days=d,hours=h,minutes=m,seconds=s)result = result_date.strftime('%Y%m%d%H%M%S')return result
以时间格式输出:
cur_time='20220909221050'
the_date = datetime.datetime.strptime(cur_time, '%Y%m%d%H%M%S') #转成时间格式
print(the_date)
# 输出结果:2022-09-09 22:10:50result = the_date.strftime('%Y%m%d%H%M%S')
result = the_date.strftime('%Y%m%d%H%M%S') #转成字符串
print(result[:4])
# 输出结果:2022
Python中的时间函数datetime.timedelta()相关推荐
- python的datetime.strptime_Python中的时间函数datetime.strptime()参数顺序的问题
即便是python资深用户也经常会错写datetime.strptime(string, format) ,颠倒两个参数的顺序而写成了datetime.strptime(format, string) ...
- 【Python】Python中的时间函数strftime与strptime对比
strftime与strptime这两个函数,初看真是一脸懵逼,不是同一个么,对于小萌新来说,多少有点晕菜了,随时可能把两者混淆,导致程序报错或者出现小bug,查询了几个小时,到最后才发现竟然一个字母 ...
- python时间函数报错_python3中datetime库,time库以及pandas中的时间函数区别与详解...
1介绍datetime库之前 我们先比较下time库和datetime库的区别 先说下time 在 Python 文档里,time是归类在Generic Operating System Servic ...
- python获取系统时间函数_python3中datetime库,time库以及pandas中的时间函数区别与详解...
1介绍datetime库之前 我们先比较下time库和datetime库的区别 先说下time 在 Python 文档里,time是归类在Generic Operating System Servic ...
- Python 中当前时间表示方法详解
在 Python 中获取当前时间是许多与时间有关的操作的一个很好的起点.一个非常重要的用例是创建时间戳.在本教程中,你将学习如何用 datetime 模块获取.显示和格式化当前时间. 我们将学习如何读 ...
- python时间比较-在python中做时间比较
时间比较是很常用的功能,python对此的支持还不错,有一个timedelta对象,保存了两个时间的差值,可以精确到微妙. 比较两个日期(年月日)的差: >>> from datet ...
- python 土拨鼠库_傻傻分不清楚系列|Python中各种时间处理方法(上)
相信大多数数据分析师在入手Python的时候,在学习到time库与datetime库时,都会对两个库里面长得很像,又相互有关联的各种类和方法感到非常窝心.当接触到pandas处理时间序列的方法时,再次 ...
- Python日期和时间函数
Python时间和日期操作需要用到datetime和time标准库模块. 一.time模块 1.time模块表示时间的方式 ①时间戳 ②格式化的时间字符串 ③以数组的形式表示,即(struc ...
- 一文搞定Python中的时间转化
一文搞定Python中的时间转化 在生活和工作中,我们每个人每天都在和时间打交道: 早上什么时候起床? 地铁几分钟来一趟? 中午什么时候开始午休? 明天是星期几? 距离上次买衣服已经2个月呢? 领导让 ...
最新文章
- Python Django 数据缓存存储位置类
- npoi 设定视图为分页预览_按班级分页打印,只要两步就行,不需要再筛选复制打印了...
- Spring,Reactor和ElasticSearch:从回调到反应流
- EMUI10安装java_linux ubuntu系统安装java jdk和配置环境,pycharm安装
- ubuntu下安装、卸载软件
- java ee会话_Java EE会话技术Cookie和Session
- java enum.isdefined_C# System.Enum.IsDefined 方法 - CSharp 参考教程
- python百分号字符串_python--003--百分号字符串拼接、format
- [BZOJ]1003 物流运输(ZJOI2006)
- 数据分析/运营——用户分层模型RFM
- 没有对象怎么面向对象编程呢?真让人头秃!
- ICPC Greater New York Region 2020 L Evenly Separated Strings
- 高数下-空间几何(一)-向量
- dbca静默建库踩坑
- 201871010123-吴丽丽《面向对象程序设计(java)》第二周学习总结
- 让lynda网站显示中文字幕
- 金砖国家智慧城市建设案例
- C语言对结构体进行赋值操作
- 升级Ember到2.2.0版本
- 一文看懂Python collections模块的高效数据类型
热门文章
- centos7/redhat7 PostgreSql搭建主从同步+Keepalived高可用 数据库高可用
- Web前端开发人员和设计师必读文章推荐【系列二】
- linux electron-**r 打开正常,配置正常,却用不了
- 除了python人工智能还可以用哪些编程语言实现?
- 昔日烟王褚时健办果园成亿万富翁 拒上市圈钱
- Git管理项目,git的基本操作语法加注释
- 如何让免费OA系统深入企业管理当中
- pytorch PixelShuffle和Upscale函数
- 香农密码理论汇总:完善保密性
- 直播间自动发言互动的思路和实现步骤,和打包成APP的方法