首先安装

pip install arrow
直接创建arrow对象
print(arrow.get(2019, 1, 23))  # 2019-01-23T00:00:00+00:00
print(arrow.Arrow(2018, 2, 24)) # 2018-02-24T00:00:00+00:00
arrow对象属性    datetime,timestamp,native,tzinfo
a = arrow.utcnow()  # 获取当前时间
print(arrow.now()) # 获取当前时间 2019-01-23T10:51:10.047906+08:00
b = a.datetime
c = a.timestamp
d = a.naive
print(a) # 2019-01-23T02:50:42.887795+00:00
print("datetime", b) # datetime 2019-01-23 03:10:34.940650+00:00
print("timestamp", c) # timestamp 1548213034
print("a.naive", d) # a.naive 2019-01-23 03:11:29.784884
获取datetime对象的值
hour = a.hour
day = a.day
print(f"hour:{hour},day:{day}") # hour:3,day:23
时间推移    a.shift(**kwargs),  shift方法获取某个时间之前或之后的时间,关键字参数为years,months,weeks,days,hours,seconds,microseconds
print("shift", a.shift(weeks=+3))  # shift 2019-02-13T03:25:29.686405+00:00
时间替换   a.replace(**kwargs) ,返回一个被替换后的arrow对象,原对象不变
print("replace", a.replace(hour=10))  # replace 2019-01-23T10:27:05.175130+00:00
格式化输出    a.format([format_string])
print("format", a.format())  # format 2019-01-23 03:28:14+00:00
print("format", a.format('YYYY-MM-DD HH:mm:ss ZZ')) # format 2019-01-23 03:29:05 +00:00
将时间戳转化为arrow对象    arrow.get(timestamp)  时间戳可以是int,float或者可以转化为float的字符串
print(arrow.get(1548211919.1432989))  # 2019-01-23T02:51:59.143299+00:00
 时间范围和区间    a.span(string), a.floor(), a.ceil()
print("a所在的时间", a)
print("a所在的时间区间", a.span("hour"))
print("a所在区间的开始", a.floor("hour"))
print("a所在区间的结尾", a.ceil("hour"))
"""
一个小时的时间区间:
a所在的时间 2019-01-23T03:39:08.401566+00:00
a所在的时间区间 (<Arrow [2019-01-23T03:00:00+00:00]>, <Arrow [2019-01-23T03:59:59.999999+00:00]>)
a所在区间的开始 2019-01-23T03:00:00+00:00
a所在区间的结尾 2019-01-23T03:59:59.999999+00:00
"""
arrow.Arrow.range 与arrow.Arrow.span_rang
import datetime

start = datetime.datetime(2018, 2, 24, 12, 30)
end = datetime.datetime(2018, 2, 24, 15, 20)
for r in arrow.Arrow.span_range('hour', start, end): # 获取start,end之间的时间区间
print(r)
for r in arrow.Arrow.range('hour', start, end): # 获取间隔单位时间的时间
print(r) """
(<Arrow [2018-02-24T12:00:00+00:00]>, <Arrow [2018-02-24T12:59:59.999999+00:00]>)
(<Arrow [2018-02-24T13:00:00+00:00]>, <Arrow [2018-02-24T13:59:59.999999+00:00]>)
(<Arrow [2018-02-24T14:00:00+00:00]>, <Arrow [2018-02-24T14:59:59.999999+00:00]>)
(<Arrow [2018-02-24T15:00:00+00:00]>, <Arrow [2018-02-24T15:59:59.999999+00:00]>)
2018-02-24T12:30:00+00:00
2018-02-24T13:30:00+00:00
2018-02-24T14:30:00+00:00
"""

参考文档:

# 官方文档  https://arrow.readthedocs.io/en/latest/

# 参考博文: https://blog.csdn.net/dagu131/article/details/79365301

python之arrow时间处理模块的更多相关文章

  1. [ Python入门教程 ] Python中日期时间datetime模块使用实例

    Python中datetime模块提供强大易用的日期处理功能,用于记录程序操作或修改时间.时间计算.日志时间显示等功能.datatime模块重新封装了time模块,提供的类包括date.time.da ...

  2. Python中的时间日期模块(time、datetime)

    目录 Datetime 获取当前时间 获取当前日期 获取当前时间的tuple元组 格式化日期和时间 时间移动 获取两个时间的时间差 时间格式转换 Time 获取距元年(1970.1.1)的秒数 当时时 ...

  3. python、js 时间日期模块time

    python 参考链接:https://www.runoob.com/python/python-date-time.html 时间戳 >>> print(time.time())# ...

  4. python模块:时间处理模块

    http://blog.csdn.net/pipisorry/article/details/53067168 常用python自带时间处理模块 python自带的时间处理模块参考[操作系统服务:ti ...

  5. 【310】◀▶ Python 日期和时间

    参考: python 时间日期计算 Python 日期和时间(菜鸟教程) 8.1. datetime — Basic date and time types python中datetime模块中dat ...

  6. 【转】Python之日期与时间处理模块(date和datetime)

    [转]Python之日期与时间处理模块(date和datetime) 本节内容 前言 相关术语的解释 时间的表现形式 time模块 datetime模块 时间格式码 总结 前言 在开发工作中,我们经常 ...

  7. Python 日期时间处理模块学习笔记

    来自:标点符的<Python 日期时间处理模块学习笔记> Python的时间处理模块在日常的使用中用的不是非常的多,但是使用的时候基本上都是要查资料,还是有些麻烦的,梳理下,便于以后方便的 ...

  8. Python时间time模块介绍

    一.明确时间元组 二.测试代码#!/usr/bin/env python # -- coding: utf-8 --' """ 时间模块,time的相关操作与测试 &qu ...

  9. Python 中的时间处理包datetime和arrow

    Python 中的时间处理包datetime和arrow 在获取贝壳分的时候用到了时间处理函数,想要获取上个月时间包括年.月.日等 # 方法一: today = datetime.date.today ...

随机推荐

  1. Windows OS PathTooLongException 转摘自http://www.cstruter.com/blog/308

    When I told one of my developer friends that I am going to write a post about the PathTooLongExcepti ...

  2. python字符串方法学习笔记

    # 一.字符串大小写转换# 字符串首字符大写print("hello world".capitalize())# 将字符串变为标题print("hello WORLD&q ...

  3. Logback配置,error和普通日志分离

    <?xml version="1.0" encoding="utf-8"?> <configuration> <springPro ...

  4. 图片查看器(类似于QQ,另外又加了JARA的下方的图片缩略导航图)

    源码地址:https://gitee.com/yolanda624/coffer/tree/master/src/components/a-photo-view

  5. XMPP即时通讯协议使用(十二)——基于xmpp搭建简单的局域网WebRTC

    创建HTML和JS ofwebrtc.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" ...

  6. Spring整合Struts2的两种方式

    https://blog.csdn.net/cuiyaoqiang/article/details/51887594

  7. python利用ConfigParser读写配置文件

    ConfigParser 是Python自带的模块, 用来读写配置文件, 用法非常简单. 配置文件的格式是: []包含的叫section,    section 下有option=value这样的键值 ...

  8. C++ 浅析调试,内存重叠查看

    这里举个例子查看内存, 环境为:vs 2017 测试为strcpy[因为测试老api,需要在 预处理中 添加 _CRT_SECURE_NO_WARNINGS ] 测试问题:内存溢出 源码: #incl ...

  9. PHP随机生成不重复的8位卡号(数字)和卡密(字符串)

    一.生成不重复的随机数字,可自定义长度(最多支持10位数) /** * 生成不重复的随机数字(不能超过10位数,否则while循环陷入死循环) * @param int $start 需要生成的数字开 ...

  10. shell中的空格【转载】

    1.定义变量时, =号的两边不可以留空格. eg: gender=femal————right gender =femal———–wrong gender= femal———–wrong gender ...