Python timedelta模块 时间增减用法

 

timedalte 是datetime中的一个对象,该对象表示两个时间的差值

构造函数:datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
其中参数都是可选,默认值为0

其中:

1 millisecond = 1000 microseconds
1 minute = 60 seconds
1 hour = 3600 seconds

1 week = 7 days

在构造函数中,注意参数值的范围:

0 <= microseconds < 1000000
0 <= seconds < 3600*24 (the number of seconds in one day)
-999999999 <= days <= 999999999

timedalte 有三个只读属性:

timedelta.min:负数最大时间差,相当于  timedelta(-999999999)。
timedelta.max:正数最大时间差,相当于  timedelta(days=999999999, hours=23, minutes=59, seconds=59, microseconds=999999)。

timedelta.resolution:两个时间的最小差值 相当于   timedelta(microseconds=1)。

 用法:

data_els = []
today_ele =datetime.now().date()
data_els.append(['今天', datetime.now().date()])
data_els.append(['昨天', today_ele - timedelta(days=1)])
data_els.append(['近7天', today_ele - timedelta(days=7)])
data_els.append(['近30天', today_ele - timedelta(days=7)])
selected = " "
for item in data_els:
option_ele = """<option value="%s" %s>%s</option> """ % (item[1], selected, item[0])
select_ele += option_ele
 
posted @ 2018-05-23 11:45  yoyo008  阅读(61716)  评论(0编辑  收藏  举报