以下是休假功能。有人可以告诉我如何根据休假时间计算休假?
def Leave(request):
if request.method == 'POST':
print request.POST
Leave = request.POST['reason']
fromdate = request.POST['fromdate']
todate = request.POST['todate']
#fromdate = datetime.datetime.strptime(fromdate, '%Y-%m-%d')
#todate = datetime.datetime.strptime(todate, '%Y-%m-%d')
e = Employee.objects.get(user = request.user)
print e.code, 'code of employee'
lh = LeaveHistory()
lh.reason = Leave
lh.from_date = fromdate
lh.to_date = todate
lh.employee = e
lh.num_of_days = ''
lh.approval_status = 'p'
lh.save()
答案 0 :(得分:0)
您只需使用日期时间减法即可完成此操作:
fromdate = '2015-06-08 17:00:00'
todate = '2015-06-23 17:00:00'
dt = datetime.strptime(todate,"%Y-%m-%d %H:%M:%S") - datetime.strptime(fromdate,"%Y-%m-%d %H:%M:%S")
print dt
打印:
15 days, 0:00:00
答案 1 :(得分:0)
你可以试试这个
tdelta = datetime.strptime(todate, FMT) - datetime.strptime(fromdate, FMT)