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

关于时间格式的问题。

  •  
  •   kenneth · 2012-12-23 23:41:48 +08:00 · 3554 次点击
    这是一个创建于 4140 天前的主题,其中的信息可能已经有所发展或是发生改变。
    用jinja2显示
    datetime.datetime.now()
    为什么时间秒还有小数点
    2012-12-23 23:37:21.392117
    求释疑。
    3 条回复    1970-01-01 08:00:00 +08:00
    LTS
        1
    LTS  
       2012-12-23 23:42:17 +08:00 via iPhone
    毫秒。
    aulara
        2
    aulara  
       2012-12-24 00:47:36 +08:00
    用strptime就解决啦
    BOYPT
        3
    BOYPT  
       2012-12-25 13:43:20 +08:00   ❤️ 1
    这问的也真够奇怪的,datetime本来就存储了到那个精度的时间数值,那显示小数不是很应该的么

    如果你需要按自己需求显示时间格式,那应该使用使用格式化了的时间,strftime,也可以给jinja2添加个filter,这样在模板里面的代码就干净利落了, 如: http://stackoverflow.com/questions/4830535/python-how-do-i-format-a-date-in-jinja2

    @aulara strptime是从字符串解析出时间(parse),strftime是时间变成字符串(format)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3795 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 00:54 · PVG 08:54 · LAX 17:54 · JFK 20:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.