Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to substract two datetime.time values in django template,and how to format a duration as hour,minutes
    primarykey
    data
    text
    <p>In a django app ,I am sending a list of <code>Entry</code> objects to the template.Each <code>Entry</code> object has a start, end times which are datetime.time values(from <code>TimeFields</code> on the form).While listing the Entry objects,I need to show the duration for each entry.Putting a duration field in model seemed to be reduntant since ,start and end times were already there</p> <p>model</p> <pre><code>class Entry(models.Model): title = models.CharField(unique=True,max_length=50) starttime=models.TimeField(null=True) endtime=models.TimeField(null=True) ... </code></pre> <p>template</p> <pre><code>{% for entry in object_list %} &lt;tr&gt; &lt;td&gt; {{entry.title}} &lt;/td&gt; &lt;td&gt; {{entry.starttime}}&lt;/td&gt; &lt;td&gt; {{entry.endtime}}&lt;/td&gt; &lt;td&gt; want to show duration here &lt;/td&gt; {%endfor %} </code></pre> <p><code>1.</code>Is there any filter which can take two datetime.time values and calculate the duration in seconds. ie,</p> <pre><code>given t1=datetime.time(2,30,50) and t2=datetime.time(3,00,50) should show 30 minutes </code></pre> <p><code>2.</code>Also,is there a filter,that can show a duration in given number of minutes as hour,minutes if the minutes value is greater than 60</p> <p>ie,</p> <pre><code>if duration is 50 minutes ==&gt; 50 minutes if duration is 150 minutes ==&gt; 2 hours,30 minutes </code></pre> <p><strong>update</strong></p> <pre><code>def diff_in_time(start,end): startdelta=datetime.timedelta(hours=start.hour,minutes=start.minute,seconds=start.second) enddelta=datetime.timedelta(hours=end.hour,minutes=end.minute,seconds=end.second) return (enddelta-startdelta).seconds/60 </code></pre> <p>when i tried with some sample time values ,it gave me the expected result</p> <pre><code>#start 11:35:00 pm #end 00:05:00 am start= datetime.time(23,35,00) end = datetime.time(00,05,00) print diff_in_time(start,end) ==&gt; 30 minutes #start 00:35:00 am #end 01:35:00 am start= datetime.time(00,35,00) end = datetime.time(01,35,00) print diff_in_time(start,end) ==&gt;60 minutes </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload