python获取当前时间、今天零点、235959点、昨天当前时间、明天的当前时间
python获取当前时间、今天零点、23:59:59点、昨天当前时间、明天的当前时间。
关注公众号“轻松学编程”了解更多。
获取当前时间、今天零点
使用timedalte。
timedalte 是datetime中的一个对象,该对象表示两个时间的差值。
构造函数:datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
其中参数都是可选,默认值为0
1 millisecond = 1000 microseconds
1 minute = 60 seconds
1 hour = 3600 seconds
1 week = 7 days
import datetime
# 获取当前时间
now = datetime.datetime.now()
# 获取今天零点
zeroToday = now - datetime.timedelta(hours=now.hour, minutes=now.minute, seconds=now.second,microseconds=now.microsecond)
# 获取23:59:59
lastToday = zeroToday + datetime.timedelta(hours=23, minutes=59, seconds=59)
# 获取前一天的当前时间
yesterdayNow = now - datetime.timedelta(hours=23, minutes=59, seconds=59)
# 获取明天的当前时间
tomorrowNow = now + datetime.timedelta(hours=23, minutes=59, seconds=59)
print('时间差',datetime.timedelta(hours=23, minutes=59, seconds=59))
print('当前时间',now)
print('今天零点',zeroToday)
print('获取23:59:59',lastToday)
print('昨天当前时间',yesterdayNow)
print('明天当前时间',tomorrowNow)
输出:
时间差 23:59:59
当前时间 2018-06-11 21:04:20.858475
今天零点 2018-06-11 00:00:00
获取23:59:59 2018-06-11 23:59:59
昨天当前时间 2018-06-10 21:04:21.858475
明天当前时间 2018-06-12 21:04:19.858475
把字符串转时间、获取8小时前的时间
import datetime
start_date = '2020-05-31 05:59:00'
# 把字符串转时间,并获取该时间8小时前的时间
a = datetime.datetime.strptime(start_date, '%Y-%m-%d %H:%M:%S') - datetime.timedelta(hours=8, minutes=0, seconds=0)
print(a)
print(type(a))
输出:
2020-05-30 21:59:00
<class ‘datetime.datetime’>
后记
【后记】为了让大家能够轻松学编程,我创建了一个公众号【轻松学编程】,里面有让你快速学会编程的文章,当然也有一些干货提高你的编程水平,也有一些编程项目适合做一些课程设计等课题。
也可加我微信【1257309054】,拉你进群,大家一起交流学习。
如果文章对您有帮助,请我喝杯咖啡吧!
公众号


关注我,我们一起成长~~
python获取当前时间、今天零点、235959点、昨天当前时间、明天的当前时间的更多相关文章
- 如何从日期对象python获取以毫秒(秒后3位小数)为单位的时间值?
要在python中,要获取具有毫秒(秒后3位小数)的日期字符串,请使用以下命令: %f 显示毫秒 import datetime # 获得当前时间 now=datetime.datetime.now( ...
- python 获取整点时间戳,半整点时间戳 ,同时将时间戳转换成 日期时间
import time, datetime def gettime(): for x in range(24): a = datetime.datetime.now().strftime(" ...
- [python实用代码片段]python获取当前时间的前一天,前一周,前一个月
python获取当前时间的前一天,前一周,前一个月. 实用python的datetime.timedelta方法,避免了有的月份是30和31等不同的情况. 获取前一个月的时间,方法实现:首先datet ...
- python获取网络时间和本地时间
今天我们来看一下如何用python获取网络时间和本地时间,直接上代码吧,代码中都有注释. python获取网络时间 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- python获取当前日期时间
转载自:https://www.cnblogs.com/wenBlog/p/6023742.html 在Python里如何获取当前的日期和时间呢?在Python语言里,我们可以通过调用什么模块或者类函 ...
- odoo开发笔记--python获取当天时间
取得时间相关的信息的话,要用到python time模块,python time模块里面有很多非常好用的功能,你可以去官方文档了解下,要取的当前时间的话,要取得当前时间的时间戳,时间戳好像是1970年 ...
- python 获取当前时间(关于time()时间问题的重要补充)
python 获取当前时间 我有的时候写程序要用到当前时间,我就想用python去取当前的时间,虽然不是很难,但是老是忘记,用一次丢一次,为了能够更好的记住,我今天特意写下python 当前时间这 ...
- Python获取当前系统时间
Python获取当前系统时间 import time #返回当前时间 def GetNowTime(): return time.strftime("%Y-%m-%d %H:%M:% ...
- Python获取当前时间_获取格式化时间_格式化日期
Python获取当前时间_获取格式化时间: Python获取当前时间: 使用 time.time( ) 获取到距离1970年1月1日的秒数(浮点数),然后传递给 localtime 获取当前时间 #使 ...
随机推荐
- Salesforce LWC学习(二十七) File Upload
本篇参考: https://developer.salesforce.com/docs/component-library/bundle/lightning-file-upload/documenta ...
- nginx 1.12 HTTPS双向认证配置
使用openssl生成相关证书: #生成CA私钥,私钥会被加密,需要设置密码 openssl genrsa -aes256 -out ca.key 2048 #生成CA证书签名请求,需要输入CA私钥密 ...
- python_多级菜单_纯循环与分支
源代码: #!/usr/bin/python3 __author__ = 'beimenchuixue' __blog__ = 'http://www.cnblogs.com/2bjiujiu/' d ...
- 记录从Winserver2012R2升级到Winserver2019
升级系统是必不可少的,最近想搞虚拟化Hyper-V:于是着手需要装一台WIN server 2019. 手头有一台Winserver 2012R2,正好拿来测试升级,此博做一个记录. 操作流程:在升级 ...
- c++ 中. 和 ->,波浪号 ~ 符号怎么用 ————很重要
参考:https://www.cnblogs.com/Simulation-Campus/p/8809999.html 1. 用在类中的析构函数之前,表示该函数是析构函数.如类A的析构函数 clas ...
- vue中,使用 es6的 ` 符号给字符串之间换行
我这里分功能是点击"复制范围",就相当于复制图上的坐标点一样的数据和格式: "复制功能"的代码如下: copyPoints() { const vm = thi ...
- DM9000网卡驱动分析(转)
s3c6410自带的DM9000网卡驱动也是基于platform设备模型. 其定义的设备资源在arch/arm/mach-s3c64xx/mach-smdk6410中.有网卡的resource res ...
- node将js中的json对象生成到新的excel表中
第一步 安装依赖包 npm install json2xls fs 第二步 创建node.js文件,文件内代码如下: var fs = require('fs'); var path = re ...
- 多测师讲解性能测试_面试题_001高级讲师肖sir
什么叫做性能测试?1. 软件的性能是软件的一种非功能特性,它关注的不是软件是否能够完成特定的功能,所以一般来说性能测试介入的时机是在功能测试完成之后.另外,由定义中的及时性可知性能也是一种指标,可以 ...
- day26 Pyhton 面向对象复习
一 class 类名(): pass 对象 object 对象 = 类名() class Person: pass print(Person)#<class '__main__.Person'& ...