Python标准库-datatime和time
Python标准库-datatime和time
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.标准库datatime
1>.datatime模块
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:y1053419035@qq.com import datetime # 返回本地时区当前时间的datetime对象
print(datetime.datetime.today()) #返回当前时间的datetime对象,时间到微秒,如果tz为None,返回和today()一样
print(datetime.datetime.now()) #没有时区的当前时间
print(datetime.datetime.utcnow()) #从一个时间戳返回一个datetime对象
print(datetime.datetime.fromtimestamp)
print(datetime.datetime.fromtimestamp(int(1559225186))) #返回一个到微秒的时间戳
print(datetime.datetime.now().timestamp()) #构造方法,year、month、day、hour、minute、second、microsecond,取datetime对象的年月日时分秒及微秒
print(datetime.datetime(2018, 9, 17, 10, 30, 43, 79043)) #返回星期的天,周一0,周日6
print(datetime.datetime.now().weekday()) #返回星期的天,周一1,周日7
print(datetime.datetime.now().isoweekday()) #返回日期date对象
print(datetime.datetime.now().date()) #返回时间time对象
print(datetime.datetime.now().time()) #修改并返回新的时间
print(datetime.datetime.now())
print(datetime.datetime.now().replace(2018,6,18)) #返回一个三元组(年,周数,周的天)
print(datetime.datetime.now())
print(datetime.datetime.now().isocalendar()) #以上代码执行结果如下:
2019-05-30 22:14:20.461607
2019-05-30 22:14:20.461607
2019-05-30 14:14:20.461607
<built-in method fromtimestamp of type object at 0x00000000587F2D90>
2019-05-30 22:06:26
1559225660.461607
2018-09-17 10:30:43.079043
3
4
2019-05-30
22:14:20.461607
2019-05-30 22:14:20.461607
2018-06-18 22:14:20.461607
2019-05-30 22:14:20.461607
(2019, 22, 4)
2>.日期格式化
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:y1053419035@qq.com """
日期格式化*
类方法strptime(date_string, format) ,返回datetime对象
对象方法strftime(format) ,返回字符串
字符串format函数格式化
""" import datetime dt = datetime.datetime.strptime("30/05/19 16:30", "%d/%m/%y %H:%M")
print(dt.strftime("%Y-%m-%d %H:%M:%S"))
print("{0:%Y}/{0:%m}/{0:%d} {0:%H}::{0:%M}::{0:%S}".format(dt)) #以上代码执行结果如下:
2019-05-30 16:30:00
2019/05/30 16::30::00
3>.timedelta对象
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:y1053419035@qq.com """
日期格式化*
类方法strptime(date_string, format) ,返回datetime对象
对象方法strftime(format) ,返回字符串
字符串format函数格式化
""" import datetime h = datetime.timedelta(hours=24) res1 = datetime.datetime.now() res2 = datetime.datetime.now() - h print(res1)
print(res2)
print((datetime.datetime.now() - res2).total_seconds()) #以上代码执行结果如下:
2019-05-30 22:25:01.440269
2019-05-29 22:25:01.440269
86400.0
二.标准库time
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:y1053419035@qq.com """
time
time.sleep(secs) 将调用线程挂起指定的秒数
"""
import time,datetime res1 = datetime.datetime.now()
time.sleep(5)
res2 = datetime.datetime.now() print(res1)
print(res2) #以上代码执行结果如下:
2019-05-30 22:27:46.400704
2019-05-30 22:27:51.400990
Python标准库-datatime和time的更多相关文章
- Python标准库14 数据库 (sqlite3)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python自带一个轻量级的关系型数据库SQLite.这一数据库使用SQL语言.S ...
- python标准库00 学习准备
Python标准库----走马观花 python有一套很有用的标准库.标准库会随着python解释器一起安装在你的电脑上的.它是python的一个组成部分.这些标准库是python为你准备的利器,可以 ...
- Python标准库:内置函数hasattr(object, name)
Python标准库:内置函数hasattr(object, name) 本函数是用来判断对象object的属性(name表示)是否存在.如果属性(name表示)存在,则返回True,否则返回False ...
- python标准库xml.etree.ElementTree的bug
使用python生成或者解析xml的方法用的最多的可能就数python标准库xml.etree.ElementTree和lxml了,在某些环境下使用xml.etree.ElementTree更方便一些 ...
- 【python】Python标准库defaultdict模块
来源:http://www.ynpxrz.com/n1031711c2023.aspx Python标准库中collections对集合类型的数据结构进行了很多拓展操作,这些操作在我们使用集合的时候会 ...
- Python标准库
Python标准库是随Python附带安装的,它包含大量极其有用的模块.熟悉Python标准库是十分重要的,因为如果你熟悉这些库中的模块,那么你的大多数问题都可以简单快捷地使用它们来解决. sys模块 ...
- Python标准库07 信号 (signal包,部分os包)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在了解了Linux的信号基础之后,Python标准库中的signal包就很容易学习 ...
- Python标准库04 文件管理 (部分os包,shutil包)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在操作系统下,用户可以通过操作系统的命令来管理文件,参考linux文件管理相关命令 ...
- Python标准库的学习准备
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python标准库是Python强大的动力所在,我们已经在前文中有所介绍.由于标准 ...
随机推荐
- springMVC和struts2有什么不同?为什么要用springMVC或者struts2?让你实现一个MVC框架大概如何设计?
[问题一:不同] (1)框架机制 1.Struts2采用Filter(StrutsPrepareAndExecuteFilter)实现,SpringMVC(DispatcherServlet)则采用S ...
- forever at your feet
A locket on a chainA bow that's made from rainA briar grows entwined with roseI've come to be foreve ...
- oracle 存储函数,更新库存
create procedure PRO_update_Goods_group_stock is v_min_stock , ); v_gg_stock , ); v_goods_no number; ...
- jwplayer :若请求不到流,则页面一直转圈请求效果
思路: 利用jwplayer onPlay(播放) .onError(出错)事件. 页面:背景图为黑色,嵌入一张背景为黑色的 git 动态图,加载页面时隐藏. 流程:若进入到onPlay 方法,则说明 ...
- Python爬虫笔记技术篇
目录 前言 requests出现中文乱码 使用代理 BeautifulSoup的使用 Selenium的使用 基础使用 Selenium获取网页动态数据赋值给BeautifulSoup Seleniu ...
- Linux配置AndroidSDK&Jenkins远程部署
最近将公司的项目部署了Jenkins持续集成,遇到了几个麻烦的点,其中之一就是将Android SDK进行配置在远程服务器(总结下来还是自己对Linux命令还不够熟悉),特此记录. 系统: Ubunt ...
- Inno Setup 检测已安装的.NET Framework 版本
翻译自:http://kynosarges.org/DotNetVersion.html 由 Jordan Russell 写的 Inno Setup 是一个伟大的安装脚本程序,但缺乏一个内置的函数来 ...
- Centos 安装mysql8
Centos 安装mysql ,转载 https://www.cnblogs.com/funbin/p/11154784.html 1.下载mysql wget -i -c https://repo. ...
- memcached源码分析二-lru
在前一篇文章中介绍了memcached中的内存管理策略slab,那么需要缓存的数据是如何使用slab的呢? 1. 缓存对象item内存分布 在memcached,每一个缓存的对象都使用一个ite ...
- Ubuntu遇到apt-get update报错:"E: Could not get lock /var/lib/apt/lists/lock"
sudo apt-get update报错:"E: Could not get lock /var/lib/apt/lists/lock" 出现此问题的原因可能是有另外一个程序在运 ...