Python 日期时间模块 Arrow库

我个人在 Python 中经常使用 arrow 模块替代datetime模块,个人认为非常好用,

文档

Github:https://github.com/crsmithdev/arrow/
Doc : https://arrow.readthedocs.io/en/latest/

安装

pip install arrow

使用例子

日常使用

# 获取当前时区当前时间,返回 arrow 对象
# <Arrow [2019-10-15T15:00:59.995577+08:00]>
t = arrow.now()
# 获取格林尼治时间,返回 arrow 对象 
# <Arrow [2019-10-15T06:59:38.185731+00:00]>
t = arrow.utcnow()
# 切换时区
t.to('local')

arrow 对象操作

# 获取当前时间戳
# 1571124174
t.timestamp
# 格式化输出
# 2019-10-15 15:22:54
t.format("YYYY-MM-DD HH:mm:ss")

# 当天的开始时间, 即:当日00:00:00
t.floor('day')
# 获取当月最初的时间,即:当月1日00:00:00
t.floor('month')
# 当天的结束时间, 即:当日23:59:59
t.ceil('day')
# 获取当月最后的时间,即:当月最后1日23:59:59
t.ceil('month')

#单位时间跨度的起止时间
#(<Arrow [2018-11-06T12:00:00+08:00]>, <Arrow [2018-11-06T12:59:59.999999+08:00]>)
t.span('hour')
# (<Arrow [2018-11-01T00:00:00+08:00]>,<Arrow [2018-11-30T23:59:59.999999+08:00]>)
t.span('month')
t.span('week')
t.span('day')

# 替换部分时间
t = t.replace(hour=0, minute=30,second=0)
t.format('YYYYMMDD HH:mm:ss')

字符串生成 arrow 对象

# arrow.Arrow(YYYY,M,D)
# 返回的是一个arrow对象
t = arrow.Arrow(2019,10,15)

# 时间戳转换
# <Arrow [2019-10-15T07:22:54+00:00]>
t = arrow.get(1571124174)
# 从类似日期的字符串转为日期格式的数据
t = arrow.get('20181010', 'YYYYMMDD').format("YYYY-MM-DD")
# 2019-10-15 07:22:54
t.format("YYYY-MM-DD--HH-mm-ss")

生成连续区间日期

start = arrow.get('2018-09-01','YYYY-MM-DD')
end = arrow.get('2018-09-10','YYYY-MM-DD')
[dt for dt in arrow.Arrow.range('day',start,end)]
[<Arrow [2018-09-01T00:00:00+00:00]>,
 <Arrow [2018-09-02T00:00:00+00:00]>,
 <Arrow [2018-09-03T00:00:00+00:00]>,
 <Arrow [2018-09-04T00:00:00+00:00]>,
 <Arrow [2018-09-05T00:00:00+00:00]>,
 <Arrow [2018-09-06T00:00:00+00:00]>,
 <Arrow [2018-09-07T00:00:00+00:00]>,
 <Arrow [2018-09-08T00:00:00+00:00]>,
 <Arrow [2018-09-09T00:00:00+00:00]>,
 <Arrow [2018-09-10T00:00:00+00:00]>]

时间推移

# 时间推移
# arrow.now().shift(days/weeks/months/years)
# 括号内取值为正则向未来移,为负则向过去移
t = arrow.now()
# 40分钟前
t = t.shift(minutes=-40)
# 三天前
t = t.shift(days=3)
# 10月后
t = t.shift(months=10)
t.format("m")

获取年月日属性

t = arrow.now()
# 年
t.year
# 月
t.month
# 日
t.day
# 时
t.hour
# 分
t.minute
# 秒
t.second
TokenOutput
YearYYYY2000, 2001, 2002 … 2012, 2013
YY00, 01, 02 … 12, 13
MonthMMMMJanuary, February, March …
MMMJan, Feb, Mar … 1
MM01, 02, 03 … 11, 12
M1, 2, 3 … 11, 12
Day of YearDDDD001, 002, 003 … 364, 365
DDD1, 2, 3 … 364, 365
Day of MonthDD01, 02, 03 … 30, 31
D1, 2, 3 … 30, 31
Do1st, 2nd, 3rd … 30th, 31st
Day of WeekddddMonday, Tuesday, Wednesday …
dddMon, Tue, Wed … 2
d1, 2, 3 … 6, 7
HourHH00, 01, 02 … 23, 24
H0, 1, 2 … 23, 24
hh01, 02, 03 … 11, 12
h1, 2, 3 … 11, 12
AM / PMAAM, PM, am, pm 1
aam, pm 1
Minutemm00, 01, 02 … 58, 59
m0, 1, 2 … 58, 59
Secondss00, 01, 02 … 58, 59
s0, 1, 2 … 58, 59
Sub-secondS…0, 02, 003, 000006, 123123123123…
TimezoneZZZAsia/Baku, Europe/Warsaw, GMT …
ZZ-07:00, -06:00 … +06:00, +07:00, +08, Z
Z-0700, -0600 … +0600, +0700, +08, Z
Seconds TimestampX1381685817, 1381685817.915482 …
ms or µs Timestampx1569980330813, 1569980330813221
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值