Python标准库-datatime和time
2024-08-22 02:25:48
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集合类型set
- 软件工程 speedsnail 第二次冲刺1次
- HDU 1028Ignatius and the Princess III(母函数简单题)
- C#接口的经典案例
- php邮件发送 phpmailer
- openssl移植
- github的访问变慢了
- 【HDOJ】2717 Catch That Cow
- 返回ipv 地址
- 转:Visual Studio进行Web性能测试- Part I
- 【特效】jquery选项卡插件,页面多个选项卡统一调用
- windows驱动之WDF---CharSample
- poj 1113 凸包周长
- 《javascript语言精粹》读书笔记 Item2 对象
- 八.django模型系统(二)之常用查询及表关系的实现
- js中 setTimeout延时0毫秒的作用
- Android Jetpack 组建介绍(一)——Lifecycler
- git使用方法----如何利用git管理代码?如何使用git将代码传到github中去
- Docker ssh server
- flask-后台布局页面搭建4