获取当前时间
# utc 时间
In [16]: arrow.utcnow()
Out[16]:
# 本地时间
In [17]: arrow.now()
Out[17]:
In [29]: arrow.now("Asia/Shanghai")
Out[29]:
In [61]: local.ctime()
Out[61]: 'Wed Apr 29 21:33:39 2020'
获取时间戳
In [18]: arrow.now().timestamp
Out[18]: 1588162957
In [19]: arrow.utcnow().timestamp
Out[19]: 1588162963
切换时区
In [55]: local = arrow.now().to("Asia/Shanghai")
In [56]: local
Out[56]:
In [57]: local.year
Out[57]: 2020
时间戳转化为Arrow对象
In [37]: arrow.get(1367900664)
Out[37]:
In [38]: arrow.get(1367900664.123456)
Out[38]:
格式化时间
In [20]: arrow.now().format()
Out[20]: u'2020-04-29 20:23:56+08:00'
In [21]: arrow.now().format("YYYY-MM-DD HH:mm")
Out[21]: u'2020-04-29 20:24'
转化为Arrow对象
In [40]: arrow.get("2020-04-29 20:23", "YYYY-MM-DD HH:mm")
Out[40]:
In [41]: arrow.get("2020-04-29 20:23")
Out[41]:
In [42]: arrow.get(2020,4,29)
Out[42]:
In [43]: arrow.get(2020,4,29,20,30)
Out[43]:
时间推移
t = arrow.now()
t.shift(days=-1) # 前一天
t.shift(weeks=-1) # 前一周
t.shift(months=-2) # 前两个月
t.shift(years=1) # 明年
时间替换
In [50]: a = arrow.now()
In [51]: a
Out[51]:
In [52]: a.replace(hour=4)
Out[52]:
对象的属性
>>> a = arrow.now()
>>> a
>>> a.timestamp
1467634629
>>> a.year
2016
>>> a.month
7
>>> a.day
4
>>> a.hour
20
>>> a.minute
17
>>> a.second
9
>>> a.microsecond
633154
>>> a.week
27