Add two time values and display in template

625 views Asked by At

I am having trouble on how to correctly display the addition of two time values in a template. Here is my code.

models.py

class UserTimesheet(models.Model):
    employee = models.ForeignKey(Employee, models.SET_NULL, blank=True, null=True)
    date = models.DateField()
    monday_start_time = models.TimeField(_(u"Start Time"), null=True, blank=True)
    monday_end_time = models.TimeField(_(u"End Time"), null=True, blank=True, )

    @property  
    def get_monday_total(self):
        if self.monday_start_time is not None and self.monday_end_time is not None:
            return self.monday_end_time - self.monday_start_time

I have tried using:

{{ get_monday_total }}
{{ UserTimesheet.get_monday_total }}

And a lot of different other things, but I cannot, for the life of me, figure out how to display this logic within a template. Any ideas?

Any and all help is grateful.


EDIT

views.py

@login_required(login_url="/login")
def manage_timesheet(request, pk):
    time = UserTimesheet.objects.all()

    queryset = UserTimesheet.objects.get(id=pk)
    form = Timesheet(instance=queryset)

    if request.method == 'POST':
        form = Timesheet(request.POST, instance=queryset)
        if form.is_valid():
            form.save()
            return redirect('/timehub')

    return render(request, 'newtimesheet/manage_timesheet.html', {"form": form, "queryset": queryset, "time": time})

What I want to output for example is 17:00 - 14:30 and output 2:30, like in a timesheet, someone comes in at 14:30 and leave at 17:00. I want to show the total amount of times worked, which in this example is 2.5 hours.

2

There are 2 answers

2
qcoumes On

The first problem is that you cannot subtract two time. A workaround is to use datetime:

from datetime import datetime, time, date

monday_start_time = time(14, 30)
monday_end_time = time(17)
delta = (datetime.combine(date.min, monday_end_time) - datetime.combine(date.min, monday_start_time))
print(delta)
# 2:30:00

Furthermore, since your passing queryset (which should be renamed since .get returns an instance, not a queryset), you should use {{ queryset.get_monday_total }} in your template.

0
Mohammad Golam Dostogir On

The main problem I can see is in your get_monday_total property method. You can not subtract 2 TimeField values like this and get a proper formatted time.

Update your get_monday_total() like this way:

from datetime import datetime  #import this if not already exists  
@property
def get_monday_total(self):
        if self.monday_start_time and self.monday_end_time:
            duration = datetime.combine(datetime.min, self.monday_end_time) - datetime.combine(datetime.min, self.monday_start_time)
            return "{:02}h {:02}m".format(duration.seconds // 3600, (duration.seconds // 60) % 60)
        return None
 #Sample Output: 08h 30m

You can use this in your template like this as per your views.py:

<p>Monday Total Time: {{ queryset.get_monday_total }}</p>

{% for timesheet in time %}
    <p>Monday Total Time: {{ timesheet.get_monday_total }}</p>
{% endfor %}