V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
dbas
V2EX  ›  Python

python 时间处理问题

  •  
  •   dbas · 2014-12-03 12:28:49 +08:00 · 3016 次点击
    这是一个创建于 3433 天前的主题,其中的信息可能已经有所发展或是发生改变。
    今天问题多了点
    2014-12-02T04:13:11.993453
    上面的时间我如何加8个小时呢
    显示成2014-12-02 12:13:11
    13 条回复    2014-12-04 09:54:27 +08:00
    yueyoum
        1
    yueyoum  
       2014-12-03 12:38:31 +08:00
    datetime.deltatime

    arrow.replace(hours+=8)
    dbas
        2
    dbas  
    OP
       2014-12-03 12:40:18 +08:00
    不是的。我现有的只是一条字符串 st = "2014-12-02T04:13:11.993453"
    SErHo
        3
    SErHo  
       2014-12-03 12:54:57 +08:00   ❤️ 1
    用 datetime.datetime.strptime(st, '%Y-%m-%dT%H:%M:%S.%f') 获取时间后再参考 https://docs.python.org/2/library/datetime.html#datetime.datetime.replace
    hahastudio
        4
    hahastudio  
       2014-12-03 13:09:15 +08:00   ❤️ 1
    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
        5
    staticor  
       2014-12-03 13:17:56 +08:00   ❤️ 1
    第一个日期是 iso格式的日期型 如果要加一个时间的话 可以用 timedelta


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

    另外, 对于时区的支持可以用楼上提到的.
    dbas
        6
    dbas  
    OP
       2014-12-03 13:26:21 +08:00
    谢谢大家。。感谢是一种美德
    yueyoum
        7
    yueyoum  
       2014-12-03 13:30:52 +08:00
    LZ 典型伸手党啊,

    不看文档
    不google
    给了提示 还是不懂!
    xiaowangge
        8
    xiaowangge  
       2014-12-03 13:59:46 +08:00
    《如何用好Google搜索引擎?》

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

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

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


    《提问的智慧》

    http://wiki.woodpecker.org.cn/moin/AskForHelp
    iT2afL0rd
        9
    iT2afL0rd  
       2014-12-03 14:45:02 +08:00
    @yueyoum 确实……
    fghzpqm
        10
    fghzpqm  
       2014-12-03 15:09:40 +08:00
    我来用正确的方式喂一下「伸手党」:

    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
        11
    4everLoveU  
       2014-12-03 15:31:52 +08:00
    晕,这个看库文档就能解决的吧?
    gateswong
        12
    gateswong  
       2014-12-04 09:52:00 +08:00
    http://lmgtfy.com/?q=python\ datetime\ add\ hours
    gateswong
        13
    gateswong  
       2014-12-04 09:54:27 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2634 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 04:34 · PVG 12:34 · LAX 21:34 · JFK 00:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.