nononoyes 发表于 2018-1-17 14:55:48

start = '17:55:31'
stop = '4:21:57'
startList = start.split(':')
stopList = stop.split(':')

#将str change to int
for i in range(len(startList)):
    startList = int(startList)
for j in range(len(stopList)):
    stopList = int(stopList)

#计算秒:
ifstopList-startList<0:
    if(stopList>0):
      stopList = stopList-1
      temp2 = stopList-startList+60
    else:
      stopList = stopList-1+60
      temp2 = stopList-startList+60
else:
    temp2 = stopList-startList
#计算分:
if stopList - startList<0:
    if(stopList>0):
      stopList = stopList -1
      temp1 =( stopList - startList+60)*60
    else:
      stopList = stopList -1+24
      temp1 =( stopList - startList+60)*60
else:
    temp1 =( stopList - startList)*60
#计算时:
if(stopList-startList>=0):
    temp0 =(stopList-startList)*3600
else:
    temp0 =(stopList-startList+24)*3600
print('时间差为:',temp0+temp1+temp2)


时间差为: 37586

haixiaotian 发表于 2018-1-17 16:08:09

def time_diff(start,stop):
        start_list = start.split(':')
        stop_list = stop.split(':')
        print(start_list,stop_list)
        start_out = int(start_list)*3600 + int(start_list)*60 + int(start_list)
        stop_out = int(stop_list)*3600 + int(stop_list)*60 + int(stop_list)
        if int(start_list) <= int(stop_list):
                result = stop_out - start_out
        else:
                result = 24*3600 + stop_out - start_out

        return result

start = input('请输入开始时间(例如:2:30:50):')
stop = input('请输入结束时间(例如:3:20:45):')
out = time_diff(start,stop)
print('时间差为%ds'%out)

漂流的茄子先生 发表于 2018-1-17 18:01:24

start =
stop =
diff = (stop-start)+(stop-start)*60+(stop-start)*3600
if stop < start:
    diff = diff + 24*3600
print(diff)

小黑大人 发表于 2018-1-17 18:08:43

start = input("请输入开始时间(h:m:s):")
stop = input("请输入结束时间(h:m:s):")
start = start.split(":")
stop = stop.split(":")
if int(stop) < int(start):
    h = int(stop) - int(start)+24
else:
    h =int(stop)-int(start)
m = int(stop)-int(start)
s = int(stop)-int(start)
time = h*3600+m*60+s
print("时间差为:",time,"s")

阿平96 发表于 2018-1-17 21:10:29

x1 = int(input('开始时间的时'))
x2 = int(input('开始时间的分'))
x3 = int(input('开始时间的秒'))
y1 = int(input('结束时间的时'))
y2 = int(input('结束时间的分'))
y3 = int(input('结束时间的秒'))
print('start = %d:%d:%d' %(x1,x2,x3))
print('stop = %d:%d:%d' %(y1,y2,y3))
if 0 < x1 < 24 and 0 < y1 < 24 and 0 < x2 < 60 and 0 < y2 < 60 and 0 < x3 < 60 and 0 < y3 < 60:
    if x1 < y1:
      time_diff = y1 * 3600 + y2 * 60 + y3 - x1 * 3600 - x2 * 60 - x3
      time_diff = str(time_diff)
      print(time_diff+'s')
    else:
      time_diff = (y1 + 24) * 3600 + y2 * 60 + y3 - x1 * 3600 - x2 * 60 - x3
      print(str(time_diff)+'s')
else:
    print('输入错误!')

无意中找到的 发表于 2018-1-17 23:44:41

def fun2(start, stop):
    startList = start.split(":")
    stopList = stop.split(":")
    startSecond = int(startList) * 3600 + int(startList) * 60 + int(startList)
    stopSecond = int(stopList) * 3600 + int(stopList) * 60 + int(stopList)

    if startSecond > stopSecond:
      stopSecond += 24 * 3600
    return stopSecond - startSecond

刚申请的帐号,等了半个小时才可以回复,睡觉去~

776667 发表于 2018-1-19 09:30:51

def fun142(start,end):
    start = int(start.split(':'))*3600 + int(start.split(':'))*60 + int(start.split(':'))
    end = int(end.split(':'))*3600 + int(end.split(':'))*60 + int(end.split(':'))
    if end >= start:
      return end - start
    return 86400 - start + end

if __name__ == '__main__':
    start = input('输入开始时间:')
    end = input('输入结束时间:')
    print('时间差为:%ss'%fun142(start,end))

ran123rhl 发表于 2018-1-19 15:55:50

import time

def time_difference(start,stop):
    tmp_date = "2010-01-10 "
    # 开始时间的时间戳
    start_date = tmp_date + start
    start_st = time.strptime(start_date, '%Y-%m-%d %H:%M:%S')
    start_time_stamp = time.mktime(start_st)

    # 结束时间的时间戳
    stop_date = tmp_date + stop
    stop_st = time.strptime(stop_date, '%Y-%m-%d %H:%M:%S')
    stop_time_stamp = time.mktime(stop_st)

    if start_time_stamp <= stop_time_stamp:
      timeis = int(stop_time_stamp - start_time_stamp)
      print(stop_time_stamp,start_time_stamp)
      print("时间差是 %d" % timeis)
    else:
      timeis = int(stop_time_stamp + 86400 - start_time_stamp)
      print(stop_time_stamp,start_time_stamp)
      print("时间差是 %d" % timeis)

ouyunfu 发表于 2018-1-21 04:17:33

a = input('start=')
b = input('stop=')
h1,m1,s1=a.split(':')
h2,m2,s2=b.split(':')
if int(h1) < int(h2):
    S1 = int(h1)*3600 + int(m1)*60 + int(s1)
    S2 = int(h2)*3600 + int(m2)*60 + int(s2)
    print('x')
else:
    S1 = int(h1) * 3600 + int(m1) * 60 + int(s1)
    S2 = (int(h2)+24) * 3600 + int(m2) * 60 + int(s2)

D = S2-S1
print('时差为:%d s'% D)

小甲鱼震哥 发表于 2018-1-21 11:33:08

start = list(map(int,input().split(":")))
stop = list(map(int,input().split(":")))
start_s = start*3600+start*60+start
stop_s = stop*3600 + stop * 60 +stop
if stop_s >= start_s:
    sub_s = stop_s-start_s
else:
    sub_s = 24*3600+stop_s-start_s
print(str(sub_s)+"s")

塔利班 发表于 2018-1-21 12:50:28

来晚了,也写个自己的答案{:5_92:}
def timeCal(t1,t2):
    l1=t1.split(':',3)
    l2=t2.split(':',3)
    L1=),int(l1),int(l1)]
    L2=),int(l2),int(l2)]
    s1=L1*3600+L1*60+L1
    s2=L2*3600+L2*60+L2
    if s2>=s1:
      time=s2-s1
    else:
      time=s2+24*3600-s1
    print("时间差:%d秒"%time)
start='17:55:31'
stop='4:21:57'
timeCal(start,stop)

鱼油127327 发表于 2018-1-31 12:12:16

def getSeconds(start_time,stop_time):
    '''给出两个时间算出时间差(以秒计)
    如果结束时间小于开始时间则结束时间在下一天
    开始和结束时间不超过1天'''
    if start_time.count(':')==2:
      if stop_time.count(':')==2:
            #计算时间差
            #先分割出时分秒
            (sta_hours,sta_mins,sta_sec)=start_time.split(':',2)
            (stop_hours,stop_mins,stop_sec)=stop_time.split(':',2)
            #把这些数字通通转化为int类型
            sta_hours = int(sta_hours)
            sta_mins = int(sta_mins)
            sta_sec = int(sta_sec)
            stop_hours = int(stop_hours)
            stop_mins = int(stop_mins)
            stop_sec = int(stop_sec)
            #分别用时分秒对应相减
            #计算秒钟差
            if stop_sec<sta_sec:
                stop_sec+=60
                stop_mins-=1
            get_sec=stop_sec-sta_sec
            #计算分钟差
            if stop_mins<sta_mins:
                stop_mins+=60
                stop_hours-=1
            get_mins=stop_mins-sta_mins
            #计算小时差
            if stop_hours<sta_hours:
                day=1
                get_hours=sta_hours-stop_hours
            else:
                get_hours=stop_hours-sta_hours
            #将时分秒按照输入时的格式输出
            if day==1:
                print('%dday:%dh:%dmin:%ds'%(day,get_hours,get_mins,get_sec))
            else:
                print('%dh:%dmin:%ds'%(get_hours,get_mins,get_sec))
            #将时分秒转换为秒钟
            if day==1:
                final_sec=3600*24+get_hours*3600+get_mins*60+get_sec
            else:
                final_sec=get_hours*3600+get_mins*60+get_sec
            print(str(final_sec))
      else:
            print('输入结束时间格式错误!')
    else:
      print('输入开始时间格式错误!')
      
   
start_time = input('请输入开始时间,格式xx:xx:xx\n')
stop_time = input('请输入结束时间,格式同上\n')
getSeconds(start_time,stop_time)

小金鱼儿 发表于 2018-2-3 10:54:01

不会的

dunhee 发表于 2018-2-7 15:43:34

def dif(start, stop):
    s0 =
    s1 =
    dif_L = - s0 for i in range(3)]
    dif_int = dif_L*3600 + dif_L*60 + dif_L
    if dif_int < 0:
      dif_int += 60*60*24
    return '时间差为:%ds' % dif_int

victor.xu 发表于 2018-4-2 21:55:14

def get_data(st):
    for i in range(len(st)):
      if st.isdecimal() or st == ':':
            t = st.partition(':')
            h = int(t)
            t = t.partition(':')
            m = int(t)
            s = int(t)
            time =
            return time
      else:
            return 'Input Error'   
def calc_sec(lis1,lis2):
    if lis1 <= lis2:
      result = 60 - lis1 + (60 - (lis1 + 1)) * 60 + lis2 + lis2 * 60 + (lis2 - (lis1 + 1)) * 3600
      return result
    else:
      temp = 60 - lis1 + (60 - (lis1 + 1)) * 60 + (24 - lis1 -1) * 3600
      temp1 = lis2 * 3600 + lis2 * 60 + lis2
      result = temp + temp1
      return result
print('时间格式为:hh:mm:ss')
start = input('请输入起始时间:\n')
start_lis = get_data(start)
end = input('请输入截至时间:\n')
end_lis = get_data(end)
result = calc_sec(start_lis,end_lis)
print('时间差为:%ds' % (result))

新手潘包邮 发表于 2018-5-8 06:31:15

def fun(start, stop):
    start = start.split(":")
    stop = stop.split(":")
    for i in start:
      start.append(int(i))
    for i in start:
      if type(i) == str:
            start.remove(i)
    for i in stop:
      stop.append(int(i))
    for i in stop:
      if type(i) == str:            
            stop.remove(i)
    time_lsit =
    sumnum = 0
    if start > stop:
      sumnum = (24 - start)*3600 + (60 - start)*60 + (60- start)
      sumnum += stop*3600 + stop*60 + stop
    else:
      a = start*3600 + start*60 + start
      b = stop*3600 + stop*60 + stop
      sumnum = a - b
    return sumnum
start = "17:55:31"
stop = "4:21:57"
fun(start, stop)

foxiangzun 发表于 2019-1-5 17:14:03

def timeCount(start, stop) :
      startHourValue = int(start.split(':'))
      stopHourValue = int(stop.split(':'))
      startMinuValue = int(start.split(':'))
      stopMinuValue = int(stop.split(':'))
      startSecValue = int(start.split(':'))
      stopSecValue = int(stop.split(':'))
      tempValue, tempValue1, tempValue2 = 0, 0, 0
      tempValue1 = startHourValue * 3600 + startMinuValue * 60 + startSecValue
      tempValue2 = stopHourValue * 3600 + stopMinuValue * 60 + stopSecValue
      if (stopHourValue > startHourValue) or (stopHourValue == startHourValue and stopMinuValue > startMinuValue) or (stopHourValue == startHourValue and stopMinuValue == startMinuValue and stopSecValue > startSecValue) :
                tempValue = tempValue2 - tempValue1
                print('时间相隔 :' + str(tempValue) + 's')
                print('换算一下为:%d小时,%d分,%d秒' % (tempValue // 3600, (tempValue % 3600) // 60, tempValue % 3600 % 60))
      else :
                tempValue = tempValue2 + (86400 - tempValue1)
                print('时间相隔 :' + str(tempValue) + 's')
                print('换算一下为:%d小时,%d分,%d秒' % (tempValue // 3600, (tempValue % 3600) // 60, tempValue % 3600 % 60))

start = input('请按\’18:02:34\'的格式输入开始时间:')
stop = input('请按以上格式输入结束时间:')
timeCount(start, stop)

咕咕鸡鸽鸽 发表于 2019-3-27 20:17:21

from datetime import datetime,timedelta

def fun142(t1,t2):
    t1 = datetime.strptime(t1,"%X")
    t2 = datetime.strptime(t2,"%X")
    if t1 > t2:
      t2 += timedelta(days=1)
    dalay = (t2 - t1).total_seconds()
    return dalay

kinkon 发表于 2022-3-11 16:13:38

def fun142(start, stop):
    sta = start.split(':')
    A = int(sta) * 3600 + int(sta) * 60 + int(sta)
    end = stop.split(':')
    B = int(end) * 3600 + int(end) * 60 + int(end)
    return '时间差为:' + str(B - A if B > A else 86400 + B - A) + 's'
   
      
start = '17:55:31'
stop = '24:21:57'

print(fun142(start, stop))
页: 1 [2]
查看完整版本: Python:每日一题 142