python 时间处理问题

2014 年 12 月 3 日
 dbas
今天问题多了点
2014-12-02T04:13:11.993453
上面的时间我如何加8个小时呢
显示成2014-12-02 12:13:11
3667 次点击
所在节点    Python
13 条回复
yueyoum
2014 年 12 月 3 日
datetime.deltatime

arrow.replace(hours+=8)
dbas
2014 年 12 月 3 日
不是的。我现有的只是一条字符串 st = "2014-12-02T04:13:11.993453"
SErHo
2014 年 12 月 3 日
用 datetime.datetime.strptime(st, '%Y-%m-%dT%H:%M:%S.%f') 获取时间后再参考 https://docs.python.org/2/library/datetime.html#datetime.datetime.replace
hahastudio
2014 年 12 月 3 日
from datetime import datetime, timedelta
d = datetime.strptime(st, '%Y-%m-%dT%H:%M:%S.%f')
d += timedelta(hours=8)
print d.strftime("%Y-%m-%d %H:%M:%S")

其实我猜你的目的是处理时区
去试试 pytz 和 dateutil
staticor
2014 年 12 月 3 日
第一个日期是 iso格式的日期型 如果要加一个时间的话 可以用 timedelta


可以参考Pydoc:
https://docs.python.org/3/library/datetime.html#datetime.tzinfo

另外, 对于时区的支持可以用楼上提到的.
dbas
2014 年 12 月 3 日
谢谢大家。。感谢是一种美德
yueyoum
2014 年 12 月 3 日
LZ 典型伸手党啊,

不看文档
不google
给了提示 还是不懂!
xiaowangge
2014 年 12 月 3 日
《如何用好Google搜索引擎?》

http://www.zhihu.com/question/20161362

《十大高明的Google搜索技巧》

http://www.williamlong.info/archives/728.html


《提问的智慧》

http://wiki.woodpecker.org.cn/moin/AskForHelp
iT2afL0rd
2014 年 12 月 3 日
@yueyoum 确实……
fghzpqm
2014 年 12 月 3 日
我来用正确的方式喂一下「伸手党」:

https://github.com/crsmithdev/arrow

In [1]: import arrow

In [2]: arrow.get('2014-12-02T04:13:11.993453')
Out[2]: <Arrow [2014-12-02T04:13:11.993453+00:00]>

In [3]: _.to('Asia/Shanghai')
Out[3]: <Arrow [2014-12-02T12:13:11.993453+08:00]>

In [4]: _.format('YYYY-MM-DD HH:mm:ss')
Out[4]: '2014-12-02 12:13:11'
4everLoveU
2014 年 12 月 3 日
晕,这个看库文档就能解决的吧?
gateswong
2014 年 12 月 4 日
http://lmgtfy.com/?q=python\ datetime\ add\ hours
gateswong
2014 年 12 月 4 日

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/151184

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX