Question

I am using external api which returns dates (as strings) in a strange format:

2012-09-26T15:44:10.376000 #timestamp
0:00:01.714000 #delta (some time duration)

I want to convert them to this format:

19 Sep 2012 17:02
0:17:34

Is there easy way to do this? I can convert them in my view or in template

edit -----------------------------------------------------------

I found solution for timestamp:

date_str = '2012-09-26T15:44:10.376000'
date = datetime.strptime(date_str.split('.')[0],'%Y-%m-%dT%H:%M:%S')
str = date.strftime('%m %b %Y %H:%M')
Was it helpful?

Solution

Install dateutil and then:

>>> import dateutil.parser
>>> dt = dateutil.parser.parse('2012-09-26T15:44:10.376000')
>>> dt.strftime('%d %b %Y %H:%M')
'26 Sep 2012 15:44'

For django, you can pass the dt object to your template and use the date filter:

{{ dt|date:"d b Y H:M" }}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top