python时间处理 time,datetime,arrow

内置time

1
2
3
4
5
6
7
8
9
10
11
12
import time
 
# 时间戳
# print(time.time()) <br># 时间戳转换系统时间 2018-04-04 11:00:55
# ctime = time.localtime(time.time())
# print(time.strftime('%Y-%m-%d %H:%M:%S',ctime))
# 睡1秒 (秒单位)
# print(time.sleep(1))
# 输出当前系统时间
# x = time.localtime()
# t = time.strftime('%Y-%m-%d %H:%M:%S', x)  # 按格式输入当前时间 , x = 元组
# print(t)  # 2018-02-06 16:37:30
1
2
3
4
5
6
7
# 转换时间数组
# dt = "2016-05-05 20:28:54"
# dt2 = time.strptime(dt,"%Y-%m-%d %H:%M:%S")
# time.struct_time(tm_year=2016, tm_mon=5, tm_mday=5, tm_hour=20, tm_min=28, tm_sec=54, tm_wday=3, tm_yday=126, tm_isdst=-1)
# 在转换日期时间
# dt3 = time.strftime("%Y-%m-%d %H:%M:%S",dt2)
# print(dt3)

  

内置datetime 可以做日期加减

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 对time模块的封装
import datetime
 
# %Y-%m-%d %H:%M:%S
# 年 月  日 时 分 秒
# 当前日期
# print('当前日期:', datetime.datetime.now())  # 当前日期: 2018-04-04 11:04:46.090568
# 日期相加 (3天后的日期)
# print(datetime.datetime.now() + datetime.timedelta(3))  # 2018-04-07 11:04:46.090568
# 日期相减 3天前
# print(datetime.datetime.now() + datetime.timedelta(-3))
# 小时相加 , -3减
# print(datetime.datetime.now() + datetime.timedelta(hours=3))
 
# 时间替换
# c_time = datetime.datetime.now()
# print(c_time.replace(year=2019))  # 2019-04-04 11:29:47.653526
 
# 数据库查询返回的时间类型,转换位 时分
# t = datetime.datetime(2018, 4, 4, 10, 48, 59, 793000).strftime('%H:%M')  # 10:48
# print(t)
 
 
# t_str = '2012-03-05 16:26:23'
# 将字符串转换为datetime 日期 string => datetime
# d = datetime.datetime.strptime(t_str, '%Y-%m-%d %H:%M:%S')
# 这样就可以进行加减了
# print(d + datetime.timedelta(-3))
 
# 2个日期比较
# d1 = datetime.datetime.strptime('2012-03-05 17:41:20', '%Y-%m-%d %H:%M:%S')
# d2 = datetime.datetime.strptime('2012-03-02 17:41:20', '%Y-%m-%d %H:%M:%S')
# delta = d1 - d2
# print(delta.days)

Arrow 库

安装   pip install arrow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import arrow
 
# 获取当前时间
now2 = arrow.utcnow()  # 时间戳 timestamp
# print(now2.timestamp)
#
now = arrow.now()
print(now.timestamp)  # 时间戳
# t = now.format('YYYY-MM-DD HH:mm')  # 2018-04-04 09:39
# print('当前时间',t)
# mm = start.format('HH:mm')  # 09:48
# 当前时间加1分钟
# n = now.shift(minutes=1)
# print('当前时间加1分钟',n.format('YYYY-MM-DD HH:mm'))
# 当前时间加1小时
# n = now.shift(hours=1)
# print('当前时间加1小时',n.format('YYYY-MM-DD HH:mm'))
# n = now.shift(months=1)
# print('当前时间加1月',n.format('YYYY-MM-DD HH:mm'))
# n = now.shift(years=1)
# print('当前时间加1年',n.format('YYYY-MM-DD HH:mm'))
# n = now.shift(days=1)
# print('当前时间加1天',n.format('YYYY-MM-DD HH:mm'))
# n = now.shift(days=-1)
# print('当前时间加-1天',n.format('YYYY-MM-DD HH:mm'))
# n = now.shift(weeks=1)
# print('当前时间加1周',n.format('YYYY-MM-DD HH:mm'))
 
# ---------------- 转换arrow对象
# 字符串转换arrow对象
# strarr = arrow.get("2017-01-20 11:30", "YYYY-MM-DD HH:mm")
# year = strarr.format('YYYY')
# print(year)
# 时间戳转换arrow对象
# sjc = arrow.get("1485937858.659424").format("YYYY-MM-DD")
# print(sjc)
# 直接生产一个Arrow对象
# aw = arrow.Arrow(2017, 2, 1)
# print(aw.format('YYYY:MM-DD HH:mm'))

 

posted @   qukaige  阅读(1582)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示