开发者

Format for DateTimeField [duplicate]

开发者 https://www.devze.com 2022-12-15 01:53 出处:网络
This question already has a开发者_开发知识库nswers here: Closed 10 years ago. Possible Duplicate:
This question already has a开发者_开发知识库nswers here: Closed 10 years ago.

Possible Duplicate:

Converting string into datetime

In Django I get this error "Enter a valid date/time in YYYY-MM-DD HH:MM[:ss[.uuuuuu]] format." when I try to assign a string "22-DEC-2009" to a DateTimeField in a model.

How is it possible to make DateTimeField accept a date string in format "22-DEC-2009"?


You can pass the input formats as input_formats argument to DateTimeField, so you can do this

# you can keep a list of formats yourself, or copy from django 1.2 version e.g.
# my_formats = fields.DEFAULT_DATETIME_INPUT_FORMATS + ['%d-%b-%Y']
# for latest django use this
from django.utils.formats import get_format
my_formats = get_format('DATETIME_INPUT_FORMATS')
field = DateTimeField(input_formats=my_formats,...)

If instead you directly want to assign a date-str to models.DateTimeField best way is to just convert it to datetime before hand e.g.

mymodel.date_of_birth = datetime.datetime.strptime("22-DEC-2009", "%d-%b-%Y")
0

精彩评论

暂无评论...
验证码 换一张
取 消