arrow是一个强大的时间处理模块。用于对时间的一些操作。
import arrow
t= arrow.now()
print(t)
a= t.datetime #datetime
b= t.timestamp #时间戳
print(a)
print(b)
print(type(a))
print(type(b))
#获取年份
print(a.year)
print(type(a.year))
#格式化
t.format("YYYY-MM-DD HH:mm:ss")
2020-08-26T16:02:36.836451+08:00
2020-08-26 16:02:36.836451+08:00
1598428956
<class 'datetime.datetime'>
<class 'int'>
2020
<class 'int'>
'2020-08-26 16:02:36'
#从string解析时间对象
strtime= '2020-08-25'
format= 'YYYY-MM-DD'
start = arrow.get(strtime, format)
start
<Arrow [2020-08-25T00:00:00+00:00]>
#获取从现在到月底,按天为间隔的时间
start= arrow.now()
end= arrow.get('2020-09-01','YYYY-MM-DD')
arrow.Arrow.range('day', start, end)# 获取间隔单位时间的时间 #可以按天'day',时'hour'...
<generator object Arrow.range at 0x7f191c1ed048>
for r in arrow.Arrow.range('day', start, end):
print(r)
2020-08-26T16:02:56.268046+08:00
2020-08-27T16:02:56.268046+08:00
2020-08-28T16:02:56.268046+08:00
2020-08-29T16:02:56.268046+08:00
2020-08-30T16:02:56.268046+08:00
2020-08-31T16:02:56.268046+08:00
list(map(abs, [-1,0,1]))
[1, 0, 1]
#生成一段连续的时间range
start= arrow.get('2020-08-25','YYYY-MM-DD')
end= arrow.get('2020-08-31','YYYY-MM-DD')
datelist= list(map(lambda dt: dt.format('YYYY-MM-DD'), arrow.Arrow.range('day', start, end)))#‘day' 'hour'...
#out: ['2020-08-25','2020-08-26','2020-08-27','2020-08-28','2020-08-29','2020-08-30','2020-08-31']
datelist
['2020-08-25',
'2020-08-26',
'2020-08-27',
'2020-08-28',
'2020-08-29',
'2020-08-30',
'2020-08-31']
待续...