https://blog.csdn.net/feiyang5260/article/details/87821901
方法1,使用datetime值比较(一般不如2好)
1 2 3 4 5 6 7 8 9 10 11 12 13 | import datetime # 范围时间 d_time1 = datetime.datetime.strptime( str (datetime.datetime.now().date()) + '8:30' , '%Y-%m-%d%H:%M' ) d_time2 = datetime.datetime.strptime( str (datetime.datetime.now().date()) + '18:33' , '%Y-%m-%d%H:%M' ) # 当前时间 n_time = datetime.datetime.now() print ( '当前时间: ' + str (n_time)) # 判断当前时间是否在范围时间内 if n_time > d_time1 and n_time<d_time2: print ( "在此区间中" ) else : print ( "不在此区间" ) |
结果如下:
方法2,时间字符串直接比大小(最好用)
如果是 7:00 一定要写成 07:00
1 2 3 4 5 6 7 8 9 | import datetime t1 = '15:40' t2 = '18:17' now = datetime.datetime.now().strftime( "%H:%M" ) print ( "当前时间:" + now) if t1 < now < t2: print ( "在此区间中" ) else : print ( '不在此区间中' ) |
结果如下:
方法3,直接将当前时间格式化成字符串然后转换成整数进行比较。(不方便舍弃)
1 2 3 4 5 6 | import time now = time.strftime( "%H%M%S" ) print ( "当前时间:" + now) #时间区间[09:35:10,18:01:01] if ( 180101 > int (time.strftime( "%H%M%S" )) > 93510 ): print ( '在此区间中' ) |
结果如下:当前时间15:51:27
、
示例
从txt读取指定时间段,判断是否在里面
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 40 41 42 43 44 45 46 47 48 49 50 51 52 | #!/usr/bin/python # -*- coding: UTF-8 -*- import datetime #时间 time_begin = "8:00" time_over = "22:00" #函数名 读取txt中指定参数内容 #函数输入 # path_txt txt文件地址 # canshu 要从txt读取的内容 # fengefu 参数名字和值的分隔符号 默认 - #函数输出 # 返回字符型结果 def readtxt(path_txt,canshu): #/home/pi/Desktop/info fengefu = "-" f = open (path_txt, mode = 'r+' , encoding = 'utf-8' ) # 打开txt文件,以‘utf-8’编码读取 lines = f.readlines() # 以行的形式进行读取文件 for line in lines: a = line.strip().split(fengefu) # x.strip()#除去每行的换行符 按照:分割 b = a[ 0 : 1 ] # list--str c = "".join(b).strip() # 去除空格 if c = = canshu: b = a[ 1 : 2 ] # 这是选取需要读取的位数 c = "".join(b).strip() # 去除空格 #print(c) return c f.close() def main(): #读取开始时间 字符型 数字需要转化 int() time_begin = readtxt( "/home/pi/Work/WorkPlace/python/2waibao/2face_lab/info" , "time_begin" ) print (time_begin) #读取结束时间 time_over = readtxt( "/home/pi/Work/WorkPlace/python/2waibao/2face_lab/info" , "time_over" ) print (time_over) #获取当前和时间 now = datetime.datetime.now().strftime( "%H:%M" ) print ( "当前时间:" + now) #比较是否在时间段内 if time_begin < now < time_over: print ( "在此区间中" ) else : print ( "不在此区间中" ) main() |
时间加减
1 2 3 4 5 6 7 8 | # data类型计算转换data #today = datetime.datetime.today().date() # today = data # yestoday = today + datetime.timedelta(days=-1) # tomorrow = today + datetime.timedelta(days=3) # print(today) # 2019-01-30 # print(yestoday)# 2019-01-29 # print(tomorrow)# 2019-01-31 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
2018-01-05 Arduino 433 自定义接受
2018-01-05 Arduino 433 自定义发射