time

  1. # python3
  2. # coding = utf-8
  3.  
  4. import time
  5.  
  6. timestamp = time.time()
  7. print('timestamp:%s, type: %s' % (timestamp, type(timestamp)))
  8.  
  9. # time.localtime() 默认使用 time.time()
  10. localtime = time.localtime()
  11. print('localtime:', localtime)
  12. print('current hour is:', localtime[3])
  13. print('current hour is:', localtime.tm_hour)
  14.  
  15. # time.asctime() 默认使用 time.localtime()
  16. print('localtime_readable:', time.asctime())
  17. # time.ctime() 默认使用 time.time()
  18. print('localtime_readable:', time.ctime())
  19.  
  20. # time.strftime 默认使用 time.localtime()
  21. print('localtime_formatted:', time.strftime('%Y-%m-%d %H:%M:%S'))
  22.  
  23. # 将格式字符串转换为时间戳
  24. test_str = '2017-08-26 12:12:12'
  25. test_timestamp = time.mktime(time.strptime(test_str, '%Y-%m-%d %H:%M:%S'))
  26. print('test_timestamp:', test_timestamp)

输出:

timestamp:1505887820.714079, type: <class 'float'>
localtime: time.struct_time(tm_year=2017, tm_mon=9, tm_mday=20, tm_hour=14, tm_min=10, tm_sec=20, tm_wday=2, tm_yday=263, tm_isdst=0)
current hour is: 14
current hour is: 14
localtime_readable: Wed Sep 20 14:10:20 2017
localtime_readable: Wed Sep 20 14:10:20 2017
localtime_formatted: 2017-09-20 14:10:20
test_timestamp: 1503720732.0

datetime

  1. # python3
  2. # coding = utf-8
  3.  
  4. import datetime
  5.  
  6. now = datetime.datetime.now()
  7. print('now:%s, type: %s' % (now, type(now)))
  8. print('now_replace',now.replace(hour=0, minute=0, second=0))
  9. print('now.day:', now.day)
  10. print('now_formatter:', now.strftime('%Y-%m-%d'))
  11. print('now_timestamp:', now.timestamp())
  12. print('old_time:', datetime.datetime(2015, 4, 29, 12, 20))
  13. print('midnight:', datetime.datetime.combine(datetime.date.today(), datetime.time.min))

输出:

now:2017-09-20 14:11:07.533828, type: <class 'datetime.datetime'>
now_replace 2017-09-20 00:00:00.533828
now.day: 20
now_formatter: 2017-09-20
now_timestamp: 1505887867.533828
old_time: 2015-04-29 12:20:00
midnight: 2017-09-20 00:00:00

参考资料:

PYTHON-基础-时间日期处理小结

python模块之time and datetime的更多相关文章

  1. python模块之time和datetime

    33.python模块之time      1.>>> time.time() 1470900847.8458395 ==>时间戳,从1970年到现在.      2.> ...

  2. Python模块之time、datetime

    python内置模块系列(一):time模块与datetime time模块是python内置查看当前时间戳的一个模块 一 time 1 获得时间戳 时间戳:通常来说,时间戳表示的是从1970年1月1 ...

  3. Python模块:time、datetime、random、os、sys、optparse

    time模块的方法: 时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量. struct_time时间元组,共有九个元素组.如下图: time.localtime([secs]): ...

  4. python学习道路(day6note)(time &datetime,random,shutil,shelve,xml处理,configparser,hashlib,logging模块,re正则表达式)

    1.tiim模块,因为方法较多我就写在code里面了,后面有注释 #!/usr/bin/env python #_*_coding:utf-8_*_ print("time".ce ...

  5. python模块概况,json/pickle,time/datetime,logging

    参考: http://www.cnblogs.com/wupeiqi/articles/5501365.html http://www.cnblogs.com/alex3714/articles/51 ...

  6. Python之日期与时间处理模块(date和datetime)

    本节内容 前言 相关术语的解释 时间的表现形式 time模块 datetime模块 时间格式码 总结 前言 在开发工作中,我们经常需要用到日期与时间,如: 作为日志信息的内容输出 计算某个功能的执行时 ...

  7. Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fabric模块

    Python第十五天  datetime模块 time模块   thread模块  threading模块  Queue队列模块  multiprocessing模块  paramiko模块  fab ...

  8. python初步学习-import和datetime模块

    模块 一个完整大型的python程序是由模块和包的形式组织起来的,可见模块在python中的重要性.模块是一种组织型式,它许多有关联(关系)的代码组织放到单独的独立文件中.简单的说,可以把模块理解为一 ...

  9. 【转】Python之日期与时间处理模块(date和datetime)

    [转]Python之日期与时间处理模块(date和datetime) 本节内容 前言 相关术语的解释 时间的表现形式 time模块 datetime模块 时间格式码 总结 前言 在开发工作中,我们经常 ...

随机推荐

  1. Array(数组)对象-->concat() 方法

    1.定义和用法 concat() 方法用于连接两个或多个字符串. 语法: string.concat(string1, string2, ..., stringX) 举例: var str1='hel ...

  2. centos7 NAT链接配置(静态ip/修改网卡名为eth0)|1

    NAT的静态ip设置并且修改网卡名为eth0 1 cd /etc/sysconfig/network-scripts/ mv eno16777736  ifcfg-eth0 #修改名称 vi eth0 ...

  3. Python pip高级用法

    1.pip 高级用法为了便于用户安装和管理第三方库和软件,越来越多的编程语言拥有自己的包管理工 具,如 nodejs 的 npm, ruby 的 gem. Python 也不例外,现在 Python ...

  4. Java课程设计之——Web前端

    主要使用的技术 Javascript/JQuery html css Jsp 前期调查 能看到的网页大致可以分为两个部分,一个是搜索的首页index.html,一个是搜索结果页/s index.htm ...

  5. jquery的焦点图片无限循环关键思维

    在循环的时候,关键的是按(下一页按钮)到最后一页的时候和按(上一页按钮)到到第一页的时候如何转换: 首先必须知道3个js方法,prepend().append()和clone(); prepend() ...

  6. Juli函数

  7. stand up meeting 12-4

    今日进步: 1.国庆答题界面和结果界面的连接完成,并能显示GetRankData API返回结果和错误题目的单词信息. 2.天赋: 完成了整个  单词挑战需要碰到的"Storage" ...

  8. 串匹配问题 (KMP算法) 详解

    串这个概念对于我们学到现在的水平来说应该是经历颇丰了,因为在C语言中我们所用到的"串"知识是在字符串那里,有了这个概念,我们再去学习串就相对而言轻松多了. 那么,现在来介绍一下字符 ...

  9. python 使用记录及问题

    编码问题 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 1: ordinal not in range(12 ...

  10. 005.Ansible de palybook简单使用

    一 Ansible Playbook简介 ansbile-playbook是一系列ansible命令的集合,利用yaml 语言编写.playbook命令根据自上而下的顺序依次执行.同时,playboo ...