如何计算3个时间输入?

时间:2019-07-07 17:37:01

标签: python datetime tkinter timedelta

我不是英语为母语的人,所以我会提前申请英语。 作为初学者,我想创建一个简单的程序来计算我一天的工作时间。到目前为止,我使用Tkinter创建了3个条目(开始时间,午餐休息时间,结束时间),每个条目都需要输入一次(%H:%M)和一个确定按钮。当我按下“确定”按钮时,它应该计算开始时间和结束时间之间的时差,然后将其与午餐时间相减。问题是,我只能计算开始时间和结束时间之间的差。它给了我一个警告:

"Expected type 'delta', got datetime instead"

如果我运行该程序,则会得到:

"TypeError: unsupported operand type(s) for -: 'datetime.timedelta' and 'datetime.datetime'"

import datetime

def ok(Event = None):
    try:
        start_to_time = datetime.datetime.strptime(str(input_start_time.get()), "%H:%M")
        lunch_to_time = datetime.datetime.strptime(str(input_lunch_time.get()), "%H:%M")
        close_to_time = datetime.datetime.strptime(str(input_closing_time.get()), "%H:%M")
    x = close_to_time - start_to_time -lunch_to_time


    print(x)

3 个答案:

答案 0 :(得分:0)

您可能有两种情况

x = (work_stop - work_start) - (lunch_stop - lunch_start)

但是午餐需要两个值

x = (work_stop - work_start) - time_spend_on_lunch

但是这里time_spend_on_lunch_start必须是timedelta而不是datetime


示例1

import datetime

work_start = datetime.datetime.strptime("8:00", "%H:%M")
work_stop =  datetime.datetime.strptime("16:00", "%H:%M")

lunch_start = datetime.datetime.strptime("12:00", "%H:%M")
lunch_stop  = datetime.datetime.strptime("13:30", "%H:%M")

x = (work_stop - work_start) - (lunch_stop - lunch_start)

print(x)

示例2

import datetime

work_start = datetime.datetime.strptime("8:00", "%H:%M")
work_stop =  datetime.datetime.strptime("16:00", "%H:%M")

time = "1:30"
time = time.split(':')
time = [int(x) for x in time]

time_spend_on_lunch = datetime.timedelta(hours=time[0], minutes=time[1])

x = (work_stop - work_start) - time_spend_on_lunch

print(x)

您还可以定义midnightdatetime转换为timedelta

import datetime

midnight = datetime.datetime.strptime("0:00", "%H:%M")

time = "1:30"
time = datetime.datetime.strptime(time, "%H:%M")

time_spend_on_lunch = time - midnight

print( time_spend_on_lunch )
print( type(time_spend_on_lunch) )

答案 1 :(得分:0)

您的问题是,当减去两个datetime对象(close_to_timestart_to_time)时,您会得到一个timedelta对象,它不再代表日期,而是2个日期之间的持续时间(例如close_to_timestart_to_time之间的小时数和分钟数)。

您只能从datetimes中减去datetimes,从timedeltas中减去timedeltas,结果将始终是timedelta。由于您吃午餐的时间不是一个特定的时间,而是一个时间段,因此它更适合用timedelta表示,您可以通过从午餐结束时间减去午餐开始时间来获得像这样的时间:

start_to_time = datetime.datetime.strptime(str(input_start_time.get()), "%H:%M")
lunch_start_to_time = datetime.datetime.strptime(str(input_lunch_start_time.get()), "%H:%M")
lunch_end_to_time = datetime.datetime.strptime(str(input_lunch_end_time.get()), "%H:%M")
close_to_time = datetime.datetime.strptime(str(input_closing_time.get()), "%H:%M")

opening_duration = close_to_time - start_to_time
lunch_duration = lunch_end_to_time - lunch_start_to_time

work_duration = opening_duration - lunch_duration

print(work_duration)

当然,在这种情况下,您需要从输入中获取午餐的开始时间和结束时间。

答案 2 :(得分:-1)

假设

  • int loopCount = 0; int LoopMaxTries = 1; while ( loopCount < LoopMaxTries /* and possibly other stuff */ ) { // at the end of the loop loopCount++; } 是您开始工作的一天中的时间
  • start_to_time是午休时间的长度
  • lunch_to_time是您完成工作的一天中的时间

这应该有效

close_to_time