天天看點

python常用的第三方庫time_python 第三方庫 時間 arrow

擷取目前時間

# 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