计算两小时之间的差异

时间:2016-01-18 07:22:16

标签: python sql django

我想使用django中的sql db计算两个时间之间的差异,时间存储在时间字段中。

我试过了:

def DesigInfo(request):  # attendance summary
  emplist = models.staff.objects.values('empId', 'name')
  fDate = request.POST.get('fromDate')
  tDate = request.POST.get('toDate')

  if request.GET.get('empId_id'):
    sel = attendance.objects.filter(empId_id=request.GET.get('empId_id'),)
    for i in sel:
        # print i.
        # print   i.outTime
        # print i.inTime.hour,i.inTime.minute,i.inTime.second - i.outTime.hour,i.outTime.minute,i.outTime.second
        ss = i.inTime.hour
        ss1 = 12 - ss
        mm = i.outTime.hour
        mm1 = (12 + mm) - 12
        print ss1 + mm1

4 个答案:

答案 0 :(得分:1)

使用日期时间对象:https://docs.python.org/2/library/datetime.html

关于主题How to get current time in Python

的好的堆栈溢出帖子
$("#write").keyup(function(event){
if(event.keyCode == 13){
        $(this).val("");
    }
});

答案 1 :(得分:1)

由于i.inTimei.outTimetime个对象,因此您无法简单地减去它们。一个好的方法是将它们转换为datetime添加日期部分(使用today(),但它与差异无关),然后减去获得timedelta对象。

delta = datetime.combine(date.today(), i.outTime) - datetime.combine(date.today(), i.inTime)

(看这里:subtract two times in python

然后,如果您想在几小时内表达delta

delta_hours = delta.days * 24 + delta.seconds / 3600.0

timedelta对象有3个属性,表示时差(天,秒和微秒)的3种不同分辨率。在最后一个表达式中,我避免添加微秒,但我认为它与您的情况无关。如果还添加delta.microseconds / 3600000000.0 请注意,简单地将秒数除以3600将仅返回小时的整数部分,从而避免分数。这取决于你的业务规则如何将其四舍五入(圆形,地板,ceil或像我一样离开小数部分)

答案 2 :(得分:1)

您可能想要使用此代码:

from datetime import datetime

#set the date and time format
date_format = "%m-%d-%Y %H:%M:%S"

#convert string to actual date and time
time1  = datetime.strptime('8-01-2008 00:00:00', date_format)
time2  = datetime.strptime('8-02-2008 01:30:00', date_format)

#find the difference between two dates
diff = time2 - time1


''' days and overall hours between two dates '''
print ('Days & Overall hours from the above two dates')
#print days
days = diff.days
print (str(days) + ' day(s)')

#print overall hours
days_to_hours = days * 24
diff_btw_two_times = (diff.seconds) / 3600
overall_hours = days_to_hours + diff_btw_two_times
print (str(overall_hours) + ' hours');



''' now print only the time difference '''
''' between two times (date is ignored) '''

print ('\nTime difference between two times (date is not considered)')

#like days there is no hours in python
#but it has seconds, finding hours from seconds is easy
#just divide it by 3600

hours = (diff.seconds) / 3600  
print (str(hours) + ' Hours')


#same for minutes just divide the seconds by 60

minutes = (diff.seconds) / 60
print (str(minutes) + ' Minutes')

#to print seconds, you know already ;)

print (str(diff.seconds) + ' secs')

答案 3 :(得分:0)

我实现的最简单方法是上面给出的Zac的评论。我正在像这样使用relativedelta

from dateutil import relativedelta
difference = relativedelta.relativedelta( date1, date2)
no_of_hours = difference.hours

但是当日子改变时,它没有给我正确的结果。因此,我使用了上面表达的方法:

no_of_hours = (difference.days * 24) + (difference.seconds / 3600)

请注意,如果date2大于date1,则将得到负值。因此,您需要滑动相对变量中日期变量的位置。