Python's timedelta: can't I just get in whatever time unit I want the value of the entire difference?

后端 未结 4 410
眼角桃花
眼角桃花 2021-01-17 10:44

I am trying to have some clever dates since a post has been made on my site (\"seconds since, hours since, weeks since, etc..\") and I\'m using datetime.timedelta difference

4条回答
  •  生来不讨喜
    2021-01-17 11:00

    Like bobince said, you could use timestamps, like this:

    # assuming ts1 and ts2 are the two datetime objects
    from time import mktime
    mktime(ts1.timetuple()) - mktime(ts2.timetuple())
    

    Although I would think this is even uglier than just calculating the seconds from the timedelta object...

提交回复
热议问题