前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python时间戳与日期互转

python时间戳与日期互转

作者头像
the5fire
发布2019-02-28 16:25:21
8540
发布2019-02-28 16:25:21
举报
文章被收录于专栏:Python程序员杂谈

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

代码语言: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 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档