总结一下python中对日期和时间的常用处理方法。

准备

import time,datetime

常用操作

输出当前的日期时间

方式一:

now = time.localtime()

print '【Output】'
print type(now)
print now
print now[:3]
【Output】
<type 'time.struct_time'>
time.struct_time(tm_year=2017, tm_mon=8, tm_mday=21, tm_hour=23, tm_min=15, tm_sec=42, tm_wday=0, tm_yday=233, tm_isdst=0)
(2017, 8, 21)

输出当前时间戳(单位:秒):

print '【Output】'
print time.time()
【Output】
1503329021.99

方式二:

now = datetime.datetime.now()
print '【Output】'
print now.strftime('%Y-%m-%d %H:%M:%S')
【Output】
2017-08-21 23:23:46

格式化输出当前时间

t = time.localtime()
print '【Output】'
print time.strftime('%Y-%m-%d %H:%M:%S',t)
time.sleep(2)
print time.strftime('%Y-%m-%d %H:%M:%S') # 如果不指定时间,输出的就是当前时间
【Output】
2017-08-21 23:17:57
2017-08-21 23:17:59

附:格式化字符串总结

  • %a 英文星期简称
  • %A 英文星期全称
  • %b 英文月份简称
  • %B 英文月份全称
  • %c 本地日期时间
  • %d 日期,1~31
  • %H 小时,0~23
  • %I 小时,0~12
  • %m 月,01~12
  • %M 分钟,0~59
  • %S 秒,0~59
  • %j 年中当天的天数
  • %w 星期数,1~7
  • %W 年中的第几周
  • %x 当天日期,格式:01/31/17
  • %X 本地的当天时间
  • %y 年份,00~99
  • %Y 年份完整拼写

字符串转为日期时间对象

t = time.strptime('2000-1-1 10:00','%Y-%m-%d %H:%M')  # 注:前后格式要保持一致,否则转换会出错
print '【Output】'
print type(t)
print t
【Output】
<type 'time.struct_time'>
time.struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=10, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=1, tm_isdst=-1)

构造datetime对象

dt = datetime.datetime(2010,1,1,23)
print '【Output】'
print type(dt)
print dt
【Output】
<type 'datetime.datetime'>
2010-01-01 23:00:00

将struct_time对象转为时间戳(秒)

now = time.localtime()
timestamp = time.mktime(now)
print '【Output】'
print timestamp
【Output】
1503329307.0

将时间戳(秒)转为struct_time对象

timestamp = 1480000000
print '【Output】'
print time.localtime(timestamp)
【Output】
time.struct_time(tm_year=2016, tm_mon=11, tm_mday=24, tm_hour=23, tm_min=6, tm_sec=40, tm_wday=3, tm_yday=329, tm_isdst=0)

随机推荐

  1. galera安装之编译安装xtrabackup 2.2.11

    ----1.编译安装percona-xtrabackup yum -y install cmake gcc gcc-c++ libaio libaio-devel automake autoconf ...

  2. Android——4.2.2 源代码文件夹结构分析

    近期公司要整android内部培训,分配给我写个培训文档.这里记录例如以下: 撰写不易,转载请注明出处:http://blog.csdn.net/jscese/article/details/4089 ...

  3. c++11 lambda(了解)

    this->send_change_equip = ([this](ChangeEquipPT channge) { send_cmd(s2c_change_equip, &channg ...

  4. Piwik网站访问统计软件安装

    Piwik是一个PHP和MySQL的开放源代码的Web统计软件. 它给你一些关于你的网站的实用统计报告,比如网页浏览人数, 访问最多的页面, 搜索引擎关键词等等… Piwik拥有众多不同功能的插件,你 ...

  5. PAT 甲级 1020 Tree Traversals (二叉树遍历)

    1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

  6. 微信公众号获取用户openId How to use cURL to get jSON data and decode the data?

    w http://stackoverflow.com/questions/16700960/how-to-use-curl-to-get-json-data-and-decode-the-data

  7. timepicker php strtotime 8hours

    https://jqueryui.com/datepicker/ w timepicker datepicker 日期 时间 选择器 <script src="static/jquer ...

  8. 剑指Offer——字符流中第一个不重复的字符

    题目描述: 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中读 ...

  9. HDFS分布式集群安装

    HDFS集群安装: 1.准备工作 虚拟机(电脑8G内存 磁盘500GB) 3台 linux系统(1台namenode 2台datanode) (1)关闭防火墙 firewall-cmd --state ...

  10. KVm中EPT逆向映射机制分析

    2017-05-30 前几天简要分析了linux remap机制,虽然还有些许瑕疵,但总算大致分析的比较清楚.今天分析下EPT下的逆向映射机制.EPT具体的工作流程可参考前面博文,本文对于EPT以及其 ...