首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >python时间戳与日期互转

python时间戳与日期互转

作者头像
the5fire
发布于 2019-02-28 16:25:21
发布于 2019-02-28 16:25:21
1.3K0
举报

因为总是会有这个需求,每次用的时候都是上网上现查,觉得很费事,不如封装成自己的函数放到工具库里。

代码语言:javascript
复制
#coding:utf-8

__author__ = 'the5fire'

'''
    parse time
'''

import time

def time2stamp(timestr, format_type='%Y-%m-%d %H:%M:%S'):
    return time.mktime(time.strptime(timestr, format_type))

def stamp2time(stamp, format_type='%Y-%m-%d %H:%M:%S'):
    return time.strftime(format_type, time.localtime(stamp))


if __name__ == '__main__':
    stamp = time.time()
    nowtime = stamp2time(stamp)
    print stamp, '-->', nowtime
    print
    stamp = time2stamp(nowtime)
    print nowtime, '-->', stamp
    print 
    print stamp, '-->', stamp2time(stamp)

-

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2012-09-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档