What was midnight yesterday as an epoch time?

前端 未结 7 1356
太阳男子
太阳男子 2020-12-09 03:08

I\'m trying to get my head around the datetime module. I know the time now as an epoch and the time an event last happened (as an epoch time). What I need to do is figure

相关标签:
7条回答
  • 2020-12-09 04:00

    In my estimation, many date and time manipulations are easier to do, and to understand, using the arrow library. This is one of them.

    Create an arbitrary date and time.

    >>> import arrow
    >>> arbitrary = arrow.get(2017,8,16,11,5)
    

    Calculate midnight_yesterday: first, midnight of arbitrary as its 'day' floor; then shift this back by one day. Display the result.

    >>> midnight_yesterday = arbitrary.floor('day').shift(days=-1)
    >>> midnight_yesterday 
    <Arrow [2017-08-15T00:00:00+00:00]>
    

    Use timestamp for the desired overall result, for Python 3.3+.

    >>> midnight_yesterday.datetime.timestamp()
    1502755200.0
    

    Or use this expression for Python 2.7. (Credit: https://stackoverflow.com/a/11743262/131187 for the latter two expressions.)

    >>> (midnight_yesterday-arrow.get(1970,1,1)).total_seconds()
    1502755200.0
    
    0 讨论(0)
提交回复
热议问题