A simple Python function that converts a Python datetime formatting string to its nearest PHP equivalent.
Python and PHP use different format string conventions when specifying datetime formats:
* Python: [https://docs.python.org/2/library/time.html#time.strftime](https://docs.python.org/2/library/time.html#time.strftime)
* PHP: [http://php.net/manual/en/function.date.php](http://php.net/manual/en/function.date.php)
Working with Django the Date, Time and DateTime widgets all use Python format strings as stored in:
* django.conf.global_settings.DATE_INPUT_FORMATS
* django.conf.global_settings.TIME_INPUT_FORMATS
* django.conf.global_settings.DATETIME_INPUT_FORMATS
but if you want to use a datetime widget like the datetimepicker here:
[http://xdsoft.net/jqplugins/datetimepicker/](http://xdsoft.net/jqplugins/datetimepicker/)
then you'll find it uses PHP format specifiers.
If you want Django and the datetimepicker to populate a field in the same way, you need to ensure they use the same format.
So I add to the Django from context the default format as follows:
`context["default_datetime_input_format"] = datetime_format_python_to_PHP(DATETIME_INPUT_FORMATS[0])`
and in the template Javascript on my form for the datetimepicker i give it:
`"format": {{default_datetime_input_format}}`
and the datetimepicker now populates the the datetime field in the same format as Django.
The Django admin site has a feature to filter objects in change list by parameters supplied in the query string. Particularly, parameters such as date\__gte and date\__lte can be used.
This example is for filtering objects in change list by a date range (the date field is called expiration_date, but you can, of course, use any other name).
As the server side (Django) already supports such filtering, all you need to do is to edit this for your needs (you can also add some DRY if you want) and put it into templates/admin/app_name/model_name/change_list.html.
Some CSS for better layout:
div.date_filter select#from_month, div.date_filter select#to_month {
margin-left: 0;
}
div.date_filter label {
margin-right: 5px;
}
Adds filtering by ranges of dates in the admin filter sidebar.
[https://github.com/coolchevy/django-datefilterspec](https://github.com/coolchevy/django-datefilterspec)
[http://coolchevy.org.ua](http://coolchevy.org.ua)
https://github.com/coolchevy/django-datefilterspec/raw/master/datefilter.png
Example:
`
from django.db import models
import datefilterspec
class Person(models.Model):
date = models.DateTimeField(default=datetime.datetime.now)
date.date_filter = True
class Admin:
list_filter = ['date']
This is useful when you need to convert a datetime.datetime.now() or datetime.date.today() into a unix epoch seconds, with microsecond precision (precision only applies to datetime.datetime, as datetime.date won't have any microseconds).
I have found this is necessary for when storing the DateTime in the models as a FloatField, in order to keep the usec/microsecond precision.
At some point, I will probably create a custom model field called DateTimeWithUSec or something like that, but for now, this will do :)
Passing datetimes from Python to a [YUI DataTable](http://developer.yahoo.com/yui/datatable/) via JSON served by [django-piston](http://bitbucket.org/jespern/django-piston/) turned out to be surprisingly rocky. This code actually works with ``YAHOO.lang.JSON.stringToDate`` (*not* ``YAHOO.util.DataSource.parseDate``) which cannot handle time zone specifiers other than "Z" or dates without timezones.
The YUI [DataSource](http://developer.yahoo.com/yui/datasource/) which uses this looks something like this - note that simply setting ``format:"date"`` does not work as that uses ``YAHOO.util.DataSource.parseDate``, which uses``Date.parse`` to do the actual conversion, which will involve browser-specific formats and as of this writing only Chrome's native ``Date`` can reliably parse ISO 8601 dates.
myDataSource.responseSchema = {
resultsList: '…',
fields: [
…
{
key: 'my_date_field',
parser: YAHOO.lang.JSON.stringToDate
},
],
…
};
This is the same as [django.forms.extras.widgets.SelectDateWidget](http://code.djangoproject.com/browser/django/trunk/django/forms/extras/widgets.py#L16) but changing the order of the rendered select boxes to: day, month, year.
Returns a list of date objects for a given number of past days, including today. Useful for summaries of recent history.
Inspired by [Template range filter](http://www.djangosnippets.org/snippets/1357/)
This is an adaption of
[django.forms.extras.widgets.SelectDateWidget](http://code.djangoproject.com/browser/django/trunk/django/forms/extras/widgets.py#L16)
which has no day dropdown - it still produces a date but with the day set to 1.
Example use
class myForm(forms.Form):
# ...
date = forms.DateField(
required=False,
widget=MonthYearWidget(years=xrange(2004,2010))
)
This is a custom widget for displaying a view only date field in the django admin.
I used it to get around this ticket:
[http://code.djangoproject.com/ticket/342](http://code.djangoproject.com/ticket/342)