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

最新文章

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

热门文章

  1. matlab学习笔记4--多媒体文件的保存和读取
  2. 离线安装pycharm数据库驱动
  3. LODOP一个任务多列,只打一列如何维护
  4. 初识Neo4j
  5. Python 内置函数--super()
  6. idea创建maven web项目需要注意的一些细节
  7. docker+k8s基础篇五
  8. LeetCode 86. 分隔链表(Partition List)
  9. laravel迁移文件中字段方法对应的数据库类型
  10. 【LEETCODE】49、数组分类,简单级别,题目:566,1089