开发者

In Python, how to I create a datetime with X hours?

开发者 https://www.devze.com 2022-12-31 03:11 出处:网络
H开发者_如何学运维ow do I create the datetime that is 24 hours before NOW()? 48 before NOW()? X hours or days before now??from datetime import datetime, timedelta

H开发者_如何学运维ow do I create the datetime that is 24 hours before NOW()? 48 before NOW()? X hours or days before now??


from datetime import datetime, timedelta

def hours_ago(hours):
    return datetime.today() - timedelta(hours=hours)

def days_ago(days):
    return datetime.today() - timedelta(days=days)


>>> from datetime import datetime, timedelta
>>> datetime.now() - timedelta(hours=24)
datetime.datetime(2010, 5, 18, 14, 5, 56, 128000)
0

精彩评论

暂无评论...
验证码 换一张
取 消