两个日期之间的django分钟

时间:2013-04-24 21:12:40

标签: python django datetime

我知道之前已经问过这个问题并且我已经完成了各种答案以及文档:http://docs.python.org/2/library/datetime.html#datetime-objects但我仍然无法计算两个日期时间字段之间的分钟数。

以下是模型:

class Activity(models.Model):
    user = models.ForeignKey(User)

    created_date = models.DateTimeField(default=datetime.now)

    activity_type = models.IntegerField(default=0) 

    def time_posted_description(self):
        now = datetime.now()
        then = datetime.fromtimestamp(self.created_date)
        tdelta = now - then
        minutes = tdelta.total_minutes()

        return minutes

观点:

def portal_game_activity_page(request):

activity = Activity.objects.filter(user=request.user).order_by('-created_date')

pending_requests_list = FriendshipRequest.objects.filter(to_user=request.user, accepted=False)
number_pending_requests = len(pending_requests_list)

return render(request, 'portal/portal_game_activity.html', {'game_activity_page': True,'number_pending_requests':number_pending_requests,'activity':activity, })

模板:

{% extends "portal/portal_base.html" %}

{% block content %}
    <section>
        <table class="table">
            <tbody>
                {% for event in activity %}
                <tr>
                    <td>{{event.user}}</td>
                    <td>{{event.created_date}}</td>
                    <td>{{event.time_posted_description}}</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>    
    </section>
{% endblock %}

非常感谢任何反馈/协助。

2 个答案:

答案 0 :(得分:3)

now = datetime.now()
then = self.created_date
tdelta = now - then
minutes = tdelta.total_seconds() / 60

return minutes

答案 1 :(得分:0)

datetime.fromtimestamp get timestamp参数:

    then = datetime.fromtimestamp(self.created_date.second)

'datetime.timedelta'对象没有属性'total_minutes'