1-Python - 日期时间

about

time

datetime

arrow

https://github.com/arrow-py/arrow

Arrow是一个Python库,它提供了一种明智且人性化的方法来创建,操作,格式化和转换日期,时间和时间戳。它实现并更新了日期时间类型,填补了功能上的空白,并提供了支持许多常见创建方案的智能模块API。简而言之,它可以帮助您以更少的导入和更少的代码来处理日期和时间。

install

pip install arrow
pip install arrow==0.17.0

usage

获取当前日期时间

import arrow

now = arrow.now()
print(now)  # 2020-12-26T11:11:25.040437+08:00
print(now.strftime('%Y-%m-%d %H:%M:%S'))  # 2020-12-26 11:11:25
print(now.strftime('%Y-%m-%d'))  # 2020-12
print(now.timestamp)  # 1608952285

以当前时间为起始时间,获取前后的日期时间

import arrow

now = arrow.now()

# shift中可选参数:years, months, days, hours, minutes, seconds, microseconds, weeks, quarters, weekday

# 获取几个月前/后
print(now.shift(months=-2))  # 2020-10-26T11:06:46.955074+08:00
print(now.shift(months=-2).strftime('%Y-%m'))  # 2020-10
print(now.shift(months=2))  # 2021-02-26T11:06:46.955074+08:00
print(now.shift(months=2).strftime('%Y-%m'))  # 2021-02

# 获取几年前/后
print(now.shift(years=2))  # 2022-12-26T11:06:46.955074+08:00
print(now.shift(years=2).strftime('%Y-%m'))  # 2022-12
print(now.shift(years=-2))  # 2018-12-26T11:06:46.955074+08:00
print(now.shift(years=-2).strftime('%Y-%m'))  # 2018-12

# 几个小时前/后
print(now.strftime('%Y-%m-%d %H:%M:%S')) # 当前时间
print(now.shift(hours=-3).strftime('%Y-%m-%d %H:%M:%S'))  # 3个小时前
print(now.shift(hours=3).strftime('%Y-%m-%d %H:%M:%S'))  # 3个小时后

时间序列

借助pandas可以实现时间序列:

import pandas as pd

# 时间间隔频率,天:D/d
result = pd.date_range(start='2022-8-8', end='2022-8-12', freq='D')
print([str(i) for i in result])
"""
['2022-08-08 00:00:00', '2022-08-09 00:00:00', '2022-08-10 00:00:00', '2022-08-11 00:00:00', '2022-08-12 00:00:00']
"""

# 时间间隔频率,间隔是多天:D/d
result = pd.date_range(start='2022-8-8', end='2022-8-12', freq='2D')
print([str(i) for i in result])
"""
['2022-08-08 00:00:00', '2022-08-10 00:00:00', '2022-08-12 00:00:00']
"""

# 时间间隔频率,小时:H/h
result = pd.date_range(start='2022-8-8 00:00:00', end='2022-8-8 6:00:00', freq='H')
print([str(i) for i in result])
"""
['2022-08-08 00:00:00', '2022-08-08 01:00:00', '2022-08-08 02:00:00', '2022-08-08 03:00:00', '2022-08-08 04:00:00', '2022-08-08 05:00:00', '2022-08-08 06:00:00']
"""

# 时间间隔频率,多个小时:H/h
result = pd.date_range(start='2022-8-8 00:00:00', end='2022-8-8 6:00:00', freq='2H')
print([str(i) for i in result])
"""
['2022-08-08 00:00:00', '2022-08-08 02:00:00', '2022-08-08 04:00:00', '2022-08-08 06:00:00']
"""

# 时间间隔频率,分钟:T/min
result = pd.date_range(start='2022-8-8 00:00:00', end='2022-8-8 00:05:00', freq='T')
print([str(i) for i in result])
"""
['2022-08-08 00:00:00', '2022-08-08 00:01:00', '2022-08-08 00:02:00', '2022-08-08 00:03:00', '2022-08-08 00:04:00', '2022-08-08 00:05:00']
"""

# 时间间隔频率,多个分钟:T/min
result = pd.date_range(start='2022-8-8 00:00:00', end='2022-8-8 00:05:00', freq='2T')
print([str(i) for i in result])
"""
['2022-08-08 00:00:00', '2022-08-08 00:02:00', '2022-08-08 00:04:00']
"""

# 时间间隔频率,秒:S
result = pd.date_range(start='2022-8-8 00:00:00', end='2022-8-8 00:00:5', freq='S')
print([str(i) for i in result])
"""
['2022-08-08 00:00:00', '2022-08-08 00:00:01', '2022-08-08 00:00:02', '2022-08-08 00:00:03', '2022-08-08 00:00:04', '2022-08-08 00:00:05']
"""

# 时间间隔频率,几秒:S
result = pd.date_range(start='2022-8-8 00:00:00', end='2022-8-8 00:00:5', freq='2S')
print([str(i) for i in result])
"""
['2022-08-08 00:00:00', '2022-08-08 00:00:02', '2022-08-08 00:00:04']
"""

# 混合的
result = pd.date_range(start='2022-8-8 00:00:00', end='2022-8-16 00:00:00', freq='2D6H')
print([str(i) for i in result])
"""
['2022-08-08 00:00:00', '2022-08-10 06:00:00', '2022-08-12 12:00:00', '2022-08-14 18:00:00']
"""

参考:


that's all,see also:

github arrow | Python使用一行代码获取上个月是几月

posted @ 2020-12-26 11:05  听雨危楼  阅读(208)  评论(0编辑  收藏  举报