time

# python3
# coding = utf-8 import time timestamp = time.time()
print('timestamp:%s, type: %s' % (timestamp, type(timestamp))) # time.localtime() 默认使用 time.time()
localtime = time.localtime()
print('localtime:', localtime)
print('current hour is:', localtime[3])
print('current hour is:', localtime.tm_hour) # time.asctime() 默认使用 time.localtime()
print('localtime_readable:', time.asctime())
# time.ctime() 默认使用 time.time()
print('localtime_readable:', time.ctime()) # time.strftime 默认使用 time.localtime()
print('localtime_formatted:', time.strftime('%Y-%m-%d %H:%M:%S')) # 将格式字符串转换为时间戳
test_str = '2017-08-26 12:12:12'
test_timestamp = time.mktime(time.strptime(test_str, '%Y-%m-%d %H:%M:%S'))
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

# python3
# coding = utf-8 import datetime now = datetime.datetime.now()
print('now:%s, type: %s' % (now, type(now)))
print('now_replace',now.replace(hour=0, minute=0, second=0))
print('now.day:', now.day)
print('now_formatter:', now.strftime('%Y-%m-%d'))
print('now_timestamp:', now.timestamp())
print('old_time:', datetime.datetime(2015, 4, 29, 12, 20))
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. "html富文本"组件:<richtext> —— 快应用原生组件

        <template> <div class="container-full"> <richtext type="html&q ...

  2. Android Google Play app signing 最终完美解决方式

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/105561341 本文出自[赵彦军的博客] 在 GooglePlay 创建 App ...

  3. Github上面拉取别人提交的PR

    在github上面协同开发,避免不了拉取别的同学的PR,那么如何拉取呢? 1.首先我们看下upstream liz@liz-PC:~/jimeng/handle-api$ git remote -v ...

  4. Spring+Hibernate整合配置 --- 比较完整的spring、hibernate 配置

    Spring+Hibernate整合配置 分类: J2EE2010-11-25 17:21 16667人阅读 评论(1) 收藏 举报 springhibernateclassactionservlet ...

  5. AJ学IOS(32)UI之Quartz2D矩阵操作和图片剪切

    AJ分享,必须精品 矩阵操作 矩阵操作:(旋转,缩放,平移) 通过矩阵操作,把画出来的东西进行形变 旋转操作 方法:CGContextRotateCTM(<#CGContextRef c#> ...

  6. ModuleNotFoundError: No module named 'sklearn.cross_validation'

    本文为CSDN博主「不服输的南瓜」的原创文章,遵循 CC 4.0 BY-SA 版权协议 原文链接 ModuleNotFoundError: No module named 'sklearn.cross ...

  7. 阿里Canal框架数据库同步-实战教程

    一.Canal简介: canal是阿里巴巴旗下的一款开源项目,纯Java开发.基于数据库增量日志解析,提供增量数据订阅&消费,目前主要支持了MySQL(也支持mariaDB). 二.背景介绍: ...

  8. [YII2] Activeform表单部分组件使用方法

    文本框:textInput(); 密码框:passwordInput(); 单选框:radio(),radioList(); 复选框:checkbox(),checkboxList(); 下拉框:dr ...

  9. 【题解】POJ3041 Asteroids - 图论 - 二分图匹配

    声明:本博客所有题解都参照了网络资料或其他博客,仅为博主想加深理解而写,如有疑问欢迎与博主讨论✧。٩(ˊᗜˋ)و✧*。 POJ3041 Asteroids 题目描述 假如你现在正处在一个 \(N*N\ ...

  10. Python自然语言处理实战核心技术与算法,Python自然语言处理,PyTorch深度学习实战【下载】

    本人买的,无私贡献给大家,无解压密码 下载地址: 链接:https://pan.baidu.com/s/1cJtnhEQSXHVMgygr8PHh9A 提取码:a54u