Arrow-一个好用的日期时间Python处理库

使用arrow处理时间格式数据

安装

pip install arrow

使用

获取当前时间
import arrow

# 获取当前utc时间
t = arrow.utcnow()
print(t)

# 获取local时间
n = arrow.now()
print(n)

通过utcnow()和now()分别获取了utc时间和local时间,最终获取的是一个Arrow时间对象,通过这个对象我们可以做各种时间转换。

时间形式转换
n = arrow.now()
# 转成时间戳
ctime = n.timestamp
# 转成指定格式的字符串
stime = n.format("YYYY-MM-DD HH:mm:ss")
print(ctime)
print(stime)
  • 字符串转为Arrow对象
# 从时间戳转化为Arrow对象
c = arrow.get(1593693858)
d = arrow.get("1593693858")
print(c)
print(d)

上面效果一样,int和str效果一样

  • 时间戳转为Arrow对象
# 字符串转为arrow对象
cstr = "2017-01-20 11:30:56"
arr = arrow.get(cstr, "YYYY-MM-DD HH:mm:ss")

时间推移

# 时间推移相关
t = arrow.now()
print(t)
yes = t.shift(days=-1)
print(yes)
yes_week = t.shift(weeks=-1)
print(yes_week)
yes_two_month = t.shift(months=-2)
print(yes_two_month)
add_one_year = t.shift(years=1)
print(add_one_year)

参考文档

官方文档

github

简书参考博客

posted @ 2020-07-16 22:31  _墨染丶  阅读(178)  评论(0编辑  收藏  举报