I am getting this datetime in this format from my database.
2016-09-13T08:46:59.953948+00:00
I want to change this date into format like
13 sep 2016 08:46:59
I have used datetime module like
import datetime
datetime.datetime.strptime(2016-09-13T08:46:59.953948+00:00, 'changing format')
But it is giving error
TypeError at /admin/help
must be string, not datetime.datetime
If you don't care about the timezone and milliseconds you can try by the following function:
from datetime import datetime
def parse_db_time_string(time_string):
date = datetime.strptime(time_string.split('.')[0], '%Y-%m-%dT%H:%M:%S')
return datetime.strftime(date, '%d %b %Y %H:%M:%S')
You can call the function with your db strings then simply like this:
old_time_string = '2016-09-13T08:46:59.953948+00:00'
new_time_string = parse_db_time_string(old_time_string)
By importing Datetime and Arrow you can convert into your required format. Arrow is a python library to format date & time.
import arrow
import datetime
today = arrow.utcnow().to('Asia/Calcutta').format('YYYY-MM-DD HH:mm:ss')
'2016-09-13 15:57:38'
current_date = datetime.datetime.strptime(today, "%Y-%m-%d %H:%M:%S").strftime('%d-%B-%Y %H:%M:%S')
'13-September-2016 15:57:38'
To know more about Arrow https://micropyramid.com/blog/python-arrow-to-show-human-friendly-time/
You can use
import datetime
# n is your time as shown in example
your_time = n.strftime("%d/%m/%y %H:%M:%S")
When trying to make human readable format I recommend django humanize
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.