开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜