推荐学习书目
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
songdg
V2EX  ›  Python

将时间转化为数值有没有更好的办法

  •  
  •   songdg · Dec 22, 2018 · 2810 views
    This topic created in 2706 days ago, the information mentioned may be changed or developed.
    tt = datetime.datetime(2018, 12, 22, 22, 30, 45)
    想得到一个整数 223045,除了 int(str(tt.hour) + str(tt.minute) + str(tt.second))之外有没有其他的方法。
    11 replies    2018-12-23 19:12:45 +08:00
    AlisaDestiny
        1
    AlisaDestiny  
       Dec 22, 2018   ❤️ 1
    tt.strftime("%H%M%S")
    takato
        2
    takato  
       Dec 22, 2018   ❤️ 1
    tt.timestamp()
    takato
        3
    takato  
       Dec 22, 2018
    抱歉,没看到想要的是特定格式的整数,如果对数字格式有要求请使用#1 答案,使用 timestamp 可以保留较高的精度。
    Kagari
        4
    Kagari  
       Dec 22, 2018
    1L 正解,或者直接存时间戳( 2L )
    Herobs
        5
    Herobs  
       Dec 22, 2018 via Android
    reduce(lambda sum, x: x + sum*100, [tt.hour, tt.minute, tt.second])

    当只有一位数的时候,你这是有 bug 的
    silkriver
        6
    silkriver  
       Dec 22, 2018   ❤️ 1
    这样也可以,python 3.6 以上
    f"{tt.hour}{tt.minute}{tt.second}"
    silkriver
        7
    silkriver  
       Dec 22, 2018
    这样写更好一点
    f"{tt:%H%M%S}"
    gainsurier
        8
    gainsurier  
       Dec 22, 2018
    直接用#2 楼的办法,用时间戳保存。
    songdg
        9
    songdg  
    OP
       Dec 23, 2018
    @AlisaDestiny 谢谢帮助。
    songdg
        10
    songdg  
    OP
       Dec 23, 2018
    @takato 感谢帮助。
    songdg
        11
    songdg  
    OP
       Dec 23, 2018
    @silkriver 多谢帮助。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   964 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 21:27 · PVG 05:27 · LAX 14:27 · JFK 17:27
    ♥ Do have faith in what you're doing.