Django - Get only date from datetime.strptime
Iazy functor's answer is very helpful. For people who want to use it as template tag:
from django import templatefrom datetime import datetimeregister = template.Library()@register.filter(name='todate')def convert_str_date(value): return datetime.strptime(value, '%Y-%m-%d').date()
Then import your template.py in your HTML and use it as:
{{ date_in_str|todate }}Output: 2021.01.22
If you want to change the date format:
{{ date_in_str|todate|date:"d N Y" }}Output: 22 Jan. 2021