Issue
I would like to convert a a values_list field with a datetime object to a date object.
.values_list('time_finished', flat=True)
gives me "2016-03-22T18:52:53.486Z" and what I would like is "2016-03-22"
Thank you!
Solution
You can use datetime.datetime.date()
method to get datetime.date
object:
>>> dt = datetime.datetime.now()
>>> dt
datetime.datetime(2016, 4, 12, 15, 54, 48, 401418)
>>> dt.date()
datetime.date(2016, 4, 12)
Use datetime.datetime.strftime
to get string:
>>> dt.strftime('%Y-%m-%d')
'2016-04-12'
[dt.date() for dt in query.values_list('time_finished', flat=True)]
Answered By - falsetru
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.