如何比较两次[重复]

问题描述 投票:0回答:1

我正在尝试在python中比较两次:

from datetime import time, datetime

start = datetime(2020, 1 , 18, 9,30)
current_time = datetime.now()

if (current_time == start):
    print("Time Matched")

现在,当我正在运行代码并且时间达到上述开始时间时,它不会显示“时间已匹配”。请帮我解决一下这段代码,我要尝试的是开始时间与我的笔记本电脑时间匹配我正在尝试执行一些代码,请购买我无法执行的代码。

python datetime timedelta
1个回答
0
投票

**您的PC以毫秒为单位的停机时间,这就是为什么您无法获得理想结果的原因**

如果您在日期时间obj中提供第二个,则- \

from datetime import time, datetime

start = datetime(2020, 1 , 18, 9,30)
current_time = datetime.now()
start_str=str(start)
current_str=str(surrent_time)
if(start_str==current_str):
      print(match)

**一般方法**

如果我们要匹配特定的部分,这是23小时格式,如果您想使用12小时格式,则可以找到diff参数answer

 start = datetime(2020, 1 , 18, 9,30)
 current = datetime.now()
 start_year=start.strftime("%Y")
 start_month=start.strftime("%m")
 start_day=start.strftime("%H")
 start_hours=start.strftime("%H")
 start_min=start.strftime("%M")
 print(start_year,start_month,start_day,start_hours,start_min)


 current_year=current.strftime("%Y")
 current_month=current.strftime("%m")
 current_day=current.strftime("%H")
 current_hours=current.strftime("%H")
 current_min=current.strftime("%M")
 print(current_year,current_month,current_day,current_hours,current_min)

if((start_year==current_year) and (start_month==current_month)  and (start_day==current_day) and (start_hours==current_hours) and (start_min==current_min)):
      print("match")

-1
投票
import datetime
start = datetime.date(2020, 1, 18)
current_time = datetime.datetime.now()
if current_time == start:
    print("match")
© www.soinside.com 2019 - 2024. All rights reserved.