Python:时间戳转换

datetime格式转换:
时间格式转化strptime/字符串格式转换strftime函数

from dateutil.parser import parse

1432196564000
13位时间戳,年月日,时分秒,毫级
转为正常,则除以1000。即十位数值为年月日时分秒

当前时间戳:time.time()
当前日期:time.ctime()

  • 将字符串的时间转换为时间戳
    a = “2013-10-10 23:40:00” 将其转换为时间数组
    import time
    timeArray = time.strptime(a, “%Y-%m-%d %H:%M:%S”)
    转换为时间戳:
    timeStamp = int(time.mktime(timeArray))
    timeStamp = 1381419600
  • 时间戳转换为指定格式日期:
    1: 利用localtime()转换为时间数组,然后格式化为需要的格式,如
    importtime
    tabc = 1432196564000
    timeArray = time.localtime(abc/1000)
    item[‘date’] = time.strftime(“%Y-%m-%d %H:%M:%S”, timeArray)
    2: import datetime
    timeStamp = 1381419600
    dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
    otherStyleTime = dateArray.strftime(“%Y-%m-%d %H:%M:%S”)
    otherStyletime = “2013-10-10 23:40:00”
  • 获取当前时间并转换为指定日期格式
    1: import time
    获得当前时间时间戳
    now = int(time.time()) ->这是时间戳
    转换为其他日期格式,如:”%Y-%m-%d %H:%M:%S”
    timeArray = time.localtime(timeStamp)
    otherStyleTime = time.strftime(“%Y-%m-%d %H:%M:%S”, timeArray)
    2: now = datetime.datetime.now() ->这是时间数组格式
    转换为指定的格式:
    otherStyleTime = now.strftime(“%Y-%m-%d %H:%M:%S”)

  • 字符串格式更改
    先转为时间数组,然后转为其他格式
    timeArray = time.strptime(a, “%Y-%m-%d %H:%M:%S”)
    otherStyleTime = time.strftime(“%Y/%m/%d %H:%M:%S”, timeArray)

参考链接:http://www.2cto.com/kf/201401/276088.html
http://blog.sina.com.cn/s/blog_771875550101jfw2.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值