How can I get hours from a Python datetime?

后端 未结 2 1086
鱼传尺愫
鱼传尺愫 2021-01-01 10:34

I have a Python datetime, d, and I want to get the number of hours since midnight as a floating point number. The best I\'ve come up with is:

h = ((((d.hour         


        
相关标签:
2条回答
  • 2021-01-01 10:56
    h = (d - d.replace(hour=0,minute=0,second=0)).seconds / 3600.
    

    ... has less division and/or multiplication

    0 讨论(0)
  • 2021-01-01 10:59
    h = d.hour + d.minute / 60. + d.second / 3600.
    

    has less brackets…

    0 讨论(0)
提交回复
热议问题