I am trying to autofill a django datetimefield using a timezone aware date.
Using Django's timezone.now()
outputs Aug. 21, 2019, 5:57 a.m.
. How can I convert this to 2019-08-21 14:30:59
?
timezone. now() useful. This function returns the current date and time as a naive datetime when USE_TZ = False and as an aware datetime when USE_TZ = True .
And, because the format is dd/mm/yyyy, we must use a slash to separate the date, month and year.
If you want to do the transformation on the backend you can use Django's built in utility dateformat
.
from django.utils import timezone, dateformat
formatted_date = dateformat.format(timezone.now(), 'Y-m-d H:i:s')
You can use the code below:
from datetime import datetime
date_string = datetime.strftime(timezone.now(), '%Y-%m-%d %H:%M:%s')
Link - Ref
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With