Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
2 ตอบกลับ
6681 มุมมอง

How to compute hours with minute between two datetime fields?

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด
import datetime

date_string = "Wed, 18 Dec 2013 09:30:00 GMT"
date = datetime.datetime.strptime(date_string, "%a, %d %b %Y %H:%M:%S %Z")
end_date_string = "Wed, 18 Dec 2013 10:15:00 GMT"
end_date = datetime.datetime.strptime(end_date_string, "%a, %d %b %Y %H:%M:%S %Z")
print (end_date-date).seconds/60, "Minutes"
# Difference can get through substracting two datetime objects directly,

 if you have input string then substract after converting to datetime object.
According to your input format change format in while strptime()
อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด


The following will work however i suggest you use the answer in your other question because the field that stores the diff can be added to a form with the float time widget to do what i think you are looking for.

time_diff = fields.Datetime.from_string(self.later_datetime) - fields.Datetime.from_string(self.earlier_datetime)
hours_diff = math.floor(round(float(time_diff.days) * 24 + (float(time_diff.seconds) / 3600)))
minute_diff = math.floor((time_diff-hours_diff)*60)
อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
3
ก.ค. 23
27286
2
มิ.ย. 22
5813
2
ก.ย. 21
4788
1
พ.ย. 20
8081
2
มี.ค. 19
6205