开发者

Format for DateTimeField [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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜