Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

python - How to format a jinja2 date of type String

I receive a Json in my yml file and then it goes to my jinja2 html file. I can use my data whithout modification but when I try filters or whatever to format my date it's failed.

Like this it's work whithout formating the date:

<p>{{rule.occurences[0]}}</p>

result :

2021-01-28T09:40:10.970Z

I want it to be like this:

2021-01-28

I tried this to format my date :

<p>{{rule.occurences[0].strftime('%Y-%m-%d')}}</p>

result :

ERROR     -- [TaskProcess] 'str object' has no attribute 'strftime'

I also tried a custom filter:

<p>{{rule.occurences[0]|datetimeformat('%d-%m-%Y')}}</p>

result :

ERROR     -- [TaskProcess] datetimeformat() takes 1 positional argument but 2 were given

And also tried this one :

<p>{{rule.occurences[0]|iso8601_to_time|datetimeformat('%d-%m-%Y')}}</p>

result :

ERROR     -- [load_file] Couldn't load file jinja2: no filter named 'iso8601_to_time'

Any idea to solve this please ?

question from:https://stackoverflow.com/questions/65942813/how-to-format-a-jinja2-date-of-type-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
import datetime
a='2021-01-28T09:40:10.970'
datetime.datetime.strptime(a[:10],'%Y-%m-%d').strftime('%Y-%m-%d')

Output:

'2021-01-28'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...