0%
一、arrow时间对象
1、将时间戳转化为arrow对象
1 2 3 4
| import arrow
print(arrow.get(1367900664)) print(arrow.get(1367900664.152325))
|
2、将字符串转化为arrow对象
1 2 3 4
| import arrow
print(arrow.get('2019-03-01 12:30:45')) print(arrow.get('June was born in May 1980', 'MMMM YYYY'))
|
3、直接创建arrow对象
1 2 3 4
| import arrow
print(arrow.get(2019, 3, 1)) print(arrow.Arrow(2019, 3, 1))
|
二、基本属性
1 2 3 4 5 6 7 8 9 10 11 12 13
| import arrow
a = arrow.utcnow()
print(a) print(a.format('YYYY-MM-DD hh:mm:ss'))
print(a.year) print(a.month) print(a.day) print(a.hour) print(a.minute) print(a.second)
|
三、常见用法
1、转换时区
1 2 3 4 5 6 7
| import arrow
a = arrow.utcnow()
print(a) print(a.to('US/Pacific')) print(a.to('US/Pacific').to('utc'))
|
2、时间替换
1 2 3 4 5 6
| import arrow
a = arrow.utcnow()
print(a) print(a.replace(hour=4, minute=40))
|
3、时间推移
1 2 3 4 5 6
| import arrow
a = arrow.utcnow()
print(a) print(a.shift(weeks=+3))
|
4、人性化输出
1 2 3 4 5 6 7 8 9 10 11 12
| import arrow
present = arrow.utcnow()
past = present.shift(hours=-1) print(past.humanize())
future = present.shift(hours=2) print(future.humanize())
future = present.shift(minutes=66) print(future.humanize(present, granularity=['hour', 'minute']))
|