Converting a datestring into a proper date from and AJAX call
|Reported by:||erik@…||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
When an AJAX call passes a date in a Django L10N/I18N site, the date will be a string, formatted according to the current users locale.
This means that on the server, Django needs to convert this string into a proper date, taking the locale of the user into account. I can't find a way to do so.
The only lead I have found was to use Python's time.strptime() function, and use Django's django.utils.formats.get_format() function to pass it the correct format. However, Django's get_format() function returns "j-n-Y" for me (user with Dutch locale), which is not a valid format for the strptime() function.
USE_L10N is True, and USE_I18N is also True.
def StringToDate(datestring): from django.utils.formats import get_format from time import strptime short_datetime_format = get_format("SHORT_DATE_FORMAT") return strptime(datestring, short_datetime_format)
ValueError: time data u'08-03-2013' does not match format 'j-n-Y'