gpt4 book ai didi

python - 如何从数据库生日字段和 date.today() 计算确切的年龄?

转载 作者:行者123 更新时间:2023-12-01 00:40:51 27 4
gpt4 key购买 nike

我有两个日期:

  1. 来自数据库的查询集,其中包含日期字段 (1992-11-15) 和 date.today()

这是我的观点.py

    def pop(request):
start = randint(0, 2)
finish = randint(2, 5)
current_year = [date.today().year, date.today().year]
context = {
'alluserprofiles': zip(Profile.objects.all()[start:finish],
current_year),
'maleuserprofiles': zip(Profile.objects.filter(gender='male'), current_year),
'femaleuserprofiles': zip(Profile.objects.filter(gender='female'), current_year),
'start': start,
'finish': finish
}
return render(request, 'users/pop.html', context)

这是我的 django 模板

   {% for profile, year in alluserprofiles %}
<div class="container fixed-top" id="pop-up">
<div class="card text-center border-light mb-3">
<img class="card-img-top" src="{{ profile.image_prof.url }}" />
<div class="card-body">
<h5> {{ profile.user }} {{ year|sub:profile.birthday.year}}

如何减去它们以获得准确的年龄?我只能进行年份减法。

非常感谢

最佳答案

重复:Subtracting Dates With Python

from datetime import datetime

birthday = datetime(1988, 2, 19, 12, 0, 0)
diff = datetime.now() - birthday
print diff
# 8954 days, 7:03:45.765329

(抱歉,我只能回答,因为我没有足够的声誉)

关于python - 如何从数据库生日字段和 date.today() 计算确切的年龄?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57364252/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com