推荐学习书目
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 · Nov 17, 2015 · 2620 views
    This topic created in 3832 days ago, the information mentioned may be changed or developed.

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

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

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

    求解答,谢谢

    6 replies    2015-11-19 20:44:29 +08:00
    iptux
        1
    iptux  
       Nov 17, 2015 via Android   ❤️ 1
    help(datetime.datetime.strftime)
    wico77
        2
    wico77  
       Nov 17, 2015
    rfc386
    imkh
        3
    imkh  
    OP
       Nov 17, 2015
    @iptux 谢谢
    imkh
        4
    imkh  
    OP
       Nov 17, 2015
    >>> 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  
       Nov 17, 2015
    >>> a1 = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%MZ")
    >>> print a1
    2015-11-16T16:59Z

    顺便也学了下…
    mrqyoung
        6
    mrqyoung  
       Nov 19, 2015
    >>> import datetime
    >>> a1 = datetime.datetime.utcnow()
    >>> a1.isoformat()
    '2015-11-19T12:45:09.390330'
    >>> a1.isoformat()[:16] + 'Z'
    '2015-11-19T12:45Z'
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2897 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 15:00 · PVG 23:00 · LAX 08:00 · JFK 11:00
    ♥ Do have faith in what you're doing.