随笔 - 402  文章 - 1 评论 - 20 阅读 - 113万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

摘自:http://www.2cto.com/kf/201406/311477.html

(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"
  
方法二:
        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"
复制代码

(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])
from datetime import datetime
print u"第%d页\t发布人:%s\t发布时间:%s\t赞:%s\n%s" %(page,story[0], datetime.fromtimestamp(int(story [2])), story[3], story [1]))
print u"第%d页\t发布人:%s\t发布时间:%s\t赞:%s\n%s" %(page, story[0], (datetime.utcfromtimestamp(float(story[2]))).strftime("%Y-%m-%d %H:%M:%S"), story[3], story[1])

运行结果:

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

 

posted on   chamie  阅读(733)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示