首页 > 其他 > 详细

时间戳转换为指定格式日期

时间:2015-10-23 16:17:13      阅读:104      评论:0      收藏:0      [点我收藏+]

(1)方法:

方法一:
        利用localtime()转换为时间数组,然后格式化为需要的格式,如
        timeStamp = 1381419600
        timeArray = time.localtime(timeStamp)
        otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
        otherStyletime == "2013-10-10 23:40:00"
  
方法二:
        importdatetime
        timeStamp = 1381419600
        dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
        otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
        otherStyletime == "2013-10-10 23:40:00"

(2)我遇到的问题是:

...
print
u"第%d页\t发布人:%s\t发布时间:%s\t赞:%s\n%s" %(page,story[0],story[2], story[3], story[1])

运行结果:

第1页    发布人:心在远方?    发布时间:1445500414    赞:11741
老妈有点多愁善感,一天问我,你以后会不会找了媳妇就忘了你妈我啊,我很严肃的跟妈妈说,妈你说什么呢,我是那种能娶到媳妇的人吗?
...

 

但是,我希望运行出来的发布时间改为指定格式,所以我改了代码,如下:

...
print u"第%d页\t发布人:%s\t发布时间:%s\t赞:%s\n%s" %(page,story[0],time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(float(story[2]))), story[3], story[1])

运行结果:

第1页    发布人:心在远方?    发布时间:2015-10-22 15:53:34    赞:11749
老妈有点多愁善感,一天问我,你以后会不会找了媳妇就忘了你妈我啊,我很严肃的跟妈妈说,妈你说什么呢,我是那种能娶到媳妇的人吗?

 

时间戳转换为指定格式日期

原文:http://www.cnblogs.com/chamie/p/4904544.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!