AnkushRasgon
AnkushRasgon

Reputation: 820

Value Error :Time data '2019-03-19T07:01:02Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'

I am using dateformat function but when i search for feb month only it shows proper data but when i search for march month it shows error and this is my timedate "date_joined": "2019-02-13T07:57:10.276212Z" which i want to format

Feb month date :"date_joined": "2019-02-13T07:57:10.276212Z",
march month date : "date_joined": "2019-03-18T08:19:55.297908Z",
dates are same but when i search i got this error on march month

This is my separate dateformat.py file in templatetag folder

from django.template import Library
import datetime

register = Library()

@register.filter(expects_localtime=True)
def dateformat(value):
    return datetime.datetime.strptime(value,"%Y-%m-%dT%H:%M:%S.%fZ")

and this is my table data line

<td>{{x.date_joined| dateformat|date:'d-m-Y' }}</td>

Error:

ValueError at /
time data '2019-03-19T07:01:02Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'

Upvotes: 0

Views: 155

Answers (1)

Bharat Gera
Bharat Gera

Reputation: 820

%f is used for microsecond precision. But your time data "2019-03-19T07:01:02Z" doesn't have microseconds. Please change your code as:

@register.filter(expects_localtime=True)
def dateformat(value):
    return datetime.datetime.strptime(value,"%Y-%m-%dT%H:%M:%SZ")

This would work!

Upvotes: 2

Related Questions