乙個訂單表,已知時間字串,需要計算出兩筆訂單之間的時間差,並找出兩筆訂單中時間差最大的乙個
import datetime
time1 =
"20201222171220360"
time2 =
"20201222171320120"
time3 =
"20201222171400360"
time4 =
"20201222171400820"
time5 =
"20201222171500060"
t1 = datetime.datetime.strptime(time1,
"%y%m%d%h%m%s%f"
)#年月日時分秒微秒
t2 = datetime.datetime.strptime(time2,
"%y%m%d%h%m%s%f"
)t3 = datetime.datetime.strptime(time3,
"%y%m%d%h%m%s%f"
)t4 = datetime.datetime.strptime(time4,
"%y%m%d%h%m%s%f"
)t5 = datetime.datetime.strptime(time5,
"%y%m%d%h%m%s%f"
)diff1 = t2-t1
diff2 = t3-t2
diff3 = t4-t3
diff4 = t5-t4
time_list =[[
'diff1'
,diff1],[
'diff2'
,diff2],[
'diff3'
,diff3],[
'diff4'
,diff4]
]time_list.sort(key=
lambda ele:ele[1]
,reverse=
true
)#根據第二個元素排序
print
(t1)
print
(t2)
print
(t3)
print
(t4)
print
(t5)
print
(diff1)
print
(diff2)
print
(diff3)
print
(diff4)
print
(diff4.seconds, diff4.microseconds)
#列印時間差秒,微秒
print
(time_list)--
----
----
----
----
----
----
----
----
--2020-12
-2217:
12:20.360000
2020-12
-2217:
13:20.120000
2020-12
-2217:
14:00.360000
2020-12
-2217:
14:00.820000
2020-12
-2217:
15:00.0600000:
00:59.7600000:
00:40.2400000:
00:00.4600000:
00:59.240000
59240000[[
'diff1'
, datetime.timedelta(seconds=
59, microseconds=
760000)]
,['diff4'
, datetime.timedelta(seconds=
59, microseconds=
240000)]
,['diff2'
, datetime.timedelta(seconds=
40, microseconds=
240000)]
,['diff3'
, datetime.timedelta(microseconds=
460000)]
]
計算時間差
計算時間差的問題,之前用的datediff datediff 返回跨兩個指定日期的日期和時間邊界數。語法datediff datepart startdate enddate 引數datepart 是規定了應在日期的哪一部分計算差額的引數。下表列出了 microsoft sql server 識別的...
計算時間差
php 初始化變數 stardate substr gstardate,0,10 enddate substr genddate,0,10 共有多少天 start mktime 0,0,0,substr stardate,5,2 substr stardate,8,2 substr stardate...
時間差計算
1 計算相差多少天,多少小時,多少分,多少秒 long nd 1000 24 60 60 一天的毫秒數 long nh 1000 60 60 一小時的毫秒數 long nm 1000 60 一分鐘的毫秒數 long ns 1000 一秒的毫秒數 date nowdate new date times...