减去两个 datetime.time

发布于 2025-01-16 22:40:14 字数 1101 浏览 1 评论 0原文

我基本上需要获取实际时间(time1)并查看到 time2 需要多少小时和分钟。 这是一个倒计时,距离时间2还有多少时间。

我一直在做这个

def schedule_task():
    exp_h = 23 
    exp_m = 5
    now = datetime.datetime.now()

    if len(str(exp_m))==1:
        exp_m=str("0")+str(exp_m)

    date_time_str = str(exp_h)+":"+str(exp_m)+":00"
    exp_now = datetime.datetime.strptime(date_time_str,"%H:%M:%S").time()
    fdate = datetime.datetime.now().strftime("%H:%M:%S")

    locl_h = now.strftime("%H")
    locl_m = now.strftime("%M")

    remain = datetime.datetime.combine(now.today(), fdate.time()) - datetime.datetime.combine(now.today(), exp_now)

    lbl_remaing.config(text="Request will be sent in "+str(remain), bg="darkgrey",fg="blue")

    if locl_h.strip()==exp_h.strip() and locl_m.strip()==exp_m.strip():
        print("run func")
    else:
        lbl_remaing.after(1000, schedule_task)

有这个错误

print("----> ",datetime.datetime.combine(now.today(), fdate.time())

  • datetime.datetime.combine(now.today(), exp_now.time())) AttributeError: 'str' 对象没有属性 'time'

i basicly need to get actual time(time1) and see how many hours and minutos to time2.
Its a countdown, how many time untill time2.

I have been making this

def schedule_task():
    exp_h = 23 
    exp_m = 5
    now = datetime.datetime.now()

    if len(str(exp_m))==1:
        exp_m=str("0")+str(exp_m)

    date_time_str = str(exp_h)+":"+str(exp_m)+":00"
    exp_now = datetime.datetime.strptime(date_time_str,"%H:%M:%S").time()
    fdate = datetime.datetime.now().strftime("%H:%M:%S")

    locl_h = now.strftime("%H")
    locl_m = now.strftime("%M")

    remain = datetime.datetime.combine(now.today(), fdate.time()) - datetime.datetime.combine(now.today(), exp_now)

    lbl_remaing.config(text="Request will be sent in "+str(remain), bg="darkgrey",fg="blue")

    if locl_h.strip()==exp_h.strip() and locl_m.strip()==exp_m.strip():
        print("run func")
    else:
        lbl_remaing.after(1000, schedule_task)

Having this error

print("----> ",datetime.datetime.combine(now.today(), fdate.time())

  • datetime.datetime.combine(now.today(), exp_now.time())) AttributeError: 'str' object has no attribute 'time'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

憧憬巴黎街头的黎明 2025-01-23 22:40:14

知道了:

start_time = datetime.time(int(exp_h), int(exp_m), 00)
stop_time = datetime.time(int(locl_h), int(locl_m), int(locl_s))
    
date = datetime.date(1, 1, 1)
datetime1 = datetime.datetime.combine(date, start_time)
datetime2 = datetime.datetime.combine(date, stop_time)
time_elapsed = datetime1 - datetime2

Got it:

start_time = datetime.time(int(exp_h), int(exp_m), 00)
stop_time = datetime.time(int(locl_h), int(locl_m), int(locl_s))
    
date = datetime.date(1, 1, 1)
datetime1 = datetime.datetime.combine(date, start_time)
datetime2 = datetime.datetime.combine(date, stop_time)
time_elapsed = datetime1 - datetime2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文