总结一下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. iOS开发之 -- 判断是否第一次登陆APP

    判断是否第一次登陆app,具体方法如下: if (![[NSUserDefaults standardUserDefaults]boolForKey:@"firstLaunch"] ...

  2. Echarts 的悬浮框tooltip显示自定义格式化

    最近做的项目用到echarts雷达图,但是由于地市过多,遇到悬浮框显示问题被遮住 如图: 可以看到上面从兴安开始数据就被遮住了 为了解决这个被遮住的悬浮框,达到tooltip自定义格式 完成后的效果如 ...

  3. Java使用BigDecimal解决浮点型运算丢失精度的问题

    @Test public void test1(){ System.out.print(0.05+0.01); } @Test public void test2(){ BigDecimal b1 = ...

  4. 厚积薄发系列之JDBC详解

    创建一个以JDBC链接数据库的程序,包含七个步骤 1.加载JDBC驱动 加载要连接的数据库的驱动到JVM 如何加载?forName(数据库驱动) MySQL:Class.forName("c ...

  5. 【教程】AI画放射图

    第一步:画矩形作图宇宙键shift 第二步:分为网格 第三步:直接选择工具 第四步:填充交叉色,这步不再敖述: 第五步:视图--轮廓:快捷键ctrl+y; 第六步:直接选择工具选择除边框以外的所有节点 ...

  6. 160311、mybatis sql语句中转义字符

    问题: 在mapper  ***.xml中的sql语句中,不能直接用大于号.小于号要用转义字符 解决方法:   1.转义字符串 小于号    <    < 大于号    >    & ...

  7. PAT 甲级 1104 sum of Number Segments

    1104. Sum of Number Segments (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Pen ...

  8. 解决CMD编译中文乱码

    chcp 是MD DOS中的命令,用了显示或设置活动的代码编号的,用法查看当前编码格式(系统默认为:936  #936表示的是gb2312) chcp 修改当前编码格式(修改为utf-8) chcp ...

  9. 2017 Multi-University Training Contest - Team 5——HDU6095&&HDU6090&&HDU

    HDU6095——Rikka with Competition 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6095 题目意思:抱歉虽然是签到题,现场 ...

  10. Cookies and Session Tracking Client Identification cookie与会话跟踪 客户端识别

    w HTTP The Definitive Guide Cookies can be used to track users as they make multiple transactions to ...