本文共 608 字,大约阅读时间需要 2 分钟。
datetime.timedelta计算2个时间的时间差:
注:
1、datetime.timedelta支持days、seconds、microseconds2、找出24小时内的数据3、找出超过1天、1月、1年的数据4、配合datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(root,f)))抓取文件修改日期进行日期比较#!/usr/bin/python
import datetime
now = datetime.datetime.now()
deltaH = datetime.timedelta(seconds=1)
deltaD = datetime.timedelta(days=1)
deltaM = datetime.timedelta(days=30)deltaY = datetime.timedelta(days=365)Hour = now - deltaH
day = now - deltaDMon = now - deltaMYear = now - deltaYprint Hour
print dayprint Monprint Year:wq转载于:https://blog.51cto.com/yangzhiming/2127930