[Solved]-Django DateTimeField()

20👍

>>> import datetime
>>> myStr = "2011-10-01 15:26"
>>> WHAT_GOES_HERE = datetime.datetime.strptime(myStr, "%Y-%m-%d %H:%M")
>>> WHAT_GOES_HERE
datetime.datetime(2011, 10, 1, 15, 26)
>>> 
👤dting

3👍

1👍

From the Django documentation:

# inserting datetime.now()
import django.utils.timezone as tz
mytable.objects.create(myDate=tz.localtime())

# inserting any date:
import pytz
import django.utils.timezone as tz
from datetime import datetime as dt
my_tz = pytz.timezone('Europe/Bucharest')
my_date = dt(2018, 8, 20, 0)
mytable.objects.create(myDate=tz.make_aware(my_date, my_tz))
👤Tavy

0👍

You can simply do the following

    myStr = '2011/10/01 15:26'

And then when creating your object just use myStr as an attribute value:

    p = mytable(myDate = myStr)
    p.save() 
👤Dhouha

Leave a comment