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
imkh
V2EX  ›  Python

python 时间转换问题,求解答

  •  
  •   imkh · 2015-11-17 00:30:18 +08:00 · 2125 次点击
    这是一个创建于 3096 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请教下各位, python 中这种时间格式

    a1 = datetime.datetime.utcnow()
    print a1
    2015-11-16 09:48:19.403063

    如何转换为这种格式
    2015-11-16T09:48Z

    求解答,谢谢

    6 条回复    2015-11-19 20:44:29 +08:00
    iptux
        1
    iptux  
       2015-11-17 00:38:18 +08:00 via Android   ❤️ 1
    help(datetime.datetime.strftime)
    wico77
        2
    wico77  
       2015-11-17 00:39:29 +08:00
    rfc386
    imkh
        3
    imkh  
    OP
       2015-11-17 00:50:14 +08:00
    @iptux 谢谢
    imkh
        4
    imkh  
    OP
       2015-11-17 00:50:33 +08:00
    >>> datetime.datetime.strftime(a1,"%Y-%m-%dT%H:%MZ")
    '2015-11-16T16:44Z'
    >>> a1 = datetime.datetime.utcnow()
    >>> print a1
    2015-11-16 16:50:19.147045
    >>> a2 = datetime.datetime.strftime(a1,"%Y-%m-%dT%H:%MZ")
    >>> print a2
    2015-11-16T16:50Z
    mimzy
        5
    mimzy  
       2015-11-17 01:00:26 +08:00
    >>> a1 = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%MZ")
    >>> print a1
    2015-11-16T16:59Z

    顺便也学了下…
    mrqyoung
        6
    mrqyoung  
       2015-11-19 20:44:29 +08:00
    >>> import datetime
    >>> a1 = datetime.datetime.utcnow()
    >>> a1.isoformat()
    '2015-11-19T12:45:09.390330'
    >>> a1.isoformat()[:16] + 'Z'
    '2015-11-19T12:45Z'
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3468 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 11:37 · PVG 19:37 · LAX 04:37 · JFK 07:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.