python之arrow时间处理模块
首先安装
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时间处理模块的更多相关文章
- [ Python入门教程 ] Python中日期时间datetime模块使用实例
Python中datetime模块提供强大易用的日期处理功能,用于记录程序操作或修改时间.时间计算.日志时间显示等功能.datatime模块重新封装了time模块,提供的类包括date.time.da ...
- Python中的时间日期模块(time、datetime)
目录 Datetime 获取当前时间 获取当前日期 获取当前时间的tuple元组 格式化日期和时间 时间移动 获取两个时间的时间差 时间格式转换 Time 获取距元年(1970.1.1)的秒数 当时时 ...
- python、js 时间日期模块time
python 参考链接:https://www.runoob.com/python/python-date-time.html 时间戳 >>> print(time.time())# ...
- python模块:时间处理模块
http://blog.csdn.net/pipisorry/article/details/53067168 常用python自带时间处理模块 python自带的时间处理模块参考[操作系统服务:ti ...
- 【310】◀▶ Python 日期和时间
参考: python 时间日期计算 Python 日期和时间(菜鸟教程) 8.1. datetime — Basic date and time types python中datetime模块中dat ...
- 【转】Python之日期与时间处理模块(date和datetime)
[转]Python之日期与时间处理模块(date和datetime) 本节内容 前言 相关术语的解释 时间的表现形式 time模块 datetime模块 时间格式码 总结 前言 在开发工作中,我们经常 ...
- Python 日期时间处理模块学习笔记
来自:标点符的<Python 日期时间处理模块学习笔记> Python的时间处理模块在日常的使用中用的不是非常的多,但是使用的时候基本上都是要查资料,还是有些麻烦的,梳理下,便于以后方便的 ...
- Python时间time模块介绍
一.明确时间元组 二.测试代码#!/usr/bin/env python # -- coding: utf-8 --' """ 时间模块,time的相关操作与测试 &qu ...
- Python 中的时间处理包datetime和arrow
Python 中的时间处理包datetime和arrow 在获取贝壳分的时候用到了时间处理函数,想要获取上个月时间包括年.月.日等 # 方法一: today = datetime.date.today ...
随机推荐
- Asp.net core 使用log4net作为日志组件,记录日志到本地。
原文:Asp.net core 使用log4net作为日志组件,记录日志到本地. GitHub demo :https://github.com/zhanglilong23/Asp.NetCore.D ...
- Java调用DB的存储过程
2015/12/7 使用数据库存储过程的java代码: try { con = (Connection) DBProxy.getConnection(null); ...
- [转]Linux Shell编程入门
转自:http://www.cnblogs.com/suyang/archive/2008/05/18/1201990.html 从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的 ...
- js运算符的优先级的顺序列表
优先级权重 运算符 17 ..[].new 16 () 15 ++.-- 14 !.~.+(单目).-(单目).typeof.void.delete 13 %.*./ 12 +(双目).-(双目) 1 ...
- struct模块的使用
原理: 将一组简单数据进行打包,转换为bytes格式发送.或者将一组bytes格式数据,进行解析. 接口使用 Struct(fmt) 功能: 生成结构化对象 参数:fmt 定制的数据结构 st.pac ...
- 转载 jQuery实现放大镜特效
效果图. <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...
- vue实现前后台交互
首先需要前台先安装一个包 cnpm install axios --save 第二还需要解决跨域问题 在settings中添加一条中间件 MIDDLEWARE = [“corsheders.middl ...
- MDK(keil)4.7中文注释乱码解决
由于编码使用不统一导致别的开发环境下的文件在MDK(keil)下打开中文显示乱码,解决这一问题需要进行码制转换, 可以先将欲打开的文件转换成UTF-8格式(如在notepad中进行转换),也可以在打开 ...
- UVa 11384 (推公式+递归)
题目: 给你1到n,现在让你将每个数变成0,每一步操作可以选取任意数一起减去一个整数,减完了不能为负数!问你最少需要几步? 巨水的题,然而为什么要写博客呢?提醒自己要记得递归函数,不要傻傻的开数组硬比 ...
- 微信jssdk安卓机分享QQ好友和QQ空间出现{"errMsg":"shareQQ:fail"}
使用ajax请求appid之类的配置,然后进行wx.config和wx.ready,苹果机上是完全OK的,但是安卓机上十次有九次是失败,只有一次能成功,百度了一下,有人说是参数有空格,有人说是微信bu ...