天天看點

Python:pendulum庫處理時間

Python

的 pendulum 庫和JavaScript 的Moment.js 庫用法很類似

文檔

https://pendulum.eustace.io/docs/

安裝

pip install pendulum      

代碼示例

import pendulum

# 1、擷取時間
print(pendulum.now())
# 2019-12-12T15:52:35.837803+08:00

print(pendulum.today())
# 2019-12-12T00:00:00+08:00

print(pendulum.tomorrow())
# 2019-12-13T00:00:00+08:00

print(pendulum.yesterday())
# 2019-12-11T00:00:00+08:00


# 2、轉字元串
print(pendulum.now().to_datetime_string())
# 2019-12-12 15:51:22

print(pendulum.now().to_date_string())
# 2019-12-12

print(pendulum.now().to_time_string())
# 22:25:05

print(pendulum.now().format('%Y-%m-%d'))
# 2019-12-12


# 3、類型測試
from datetime import datetime
dt =pendulum.datetime(2015, 2, 5)
print(isinstance(dt, datetime))
True


# 4、解析規範的時間
print(pendulum.from_format('2019-12-12', '%Y-%m-%d'))
# 2019-12-12T00:00:00+00:00

print(pendulum.parse('2019-12-12'))
# 2019-12-12T00:00:00+00:00


# 6、屬性
now = pendulum.now()
print(now.year)
print(now.month)
print(now.day)
print(now.hour)
print(now.minute)
print(now.second)
# 2019  12  12  22  22 45


# 7、時間加減
now = pendulum.now()
print(now)
# 2019-12-12T22:27:48.429761+08:00

print(now.add(years=1))
# 2020-12-12T22:27:48.429761+08:00

print(now.subtract(years=1))
# 2018-12-12T22:27:48.429761+08:00

# 時間跨度計算
print(now.diff(now.add(years=1)).in_years())
# 1


# 8、設定語言地區
pendulum.set_locale('zh')

print(pendulum.now().subtract(days=1).diff_for_humans())
# 1天前

print(pendulum.now().subtract(hours=1).diff_for_humans())
# 1小時前

# 9、生成時間序列
period = pendulum.period(pendulum.now(), pendulum.now().add(days=3))

# years, months, weeks, days, hours, minutes and seconds
for dt in period.range('days'):
    print(dt)

"""
2019-12-12T22:39:42.142193+08:00
2019-12-13T22:39:42.142193+08:00
2019-12-14T22:39:42.142193+08:00
2019-12-15T22:39:42.142193+08:00
"""
      

其他示例

1、擷取本周的周一和周日

Python:pendulum庫處理時間
import pendulum

now = pendulum.now()
print(now.to_date_string())
# 2021-01-14

print(now.start_of("week").to_date_string())
# 2021-01-11

print(now.end_of("week").to_date_string())
# 2021-01-17