format_date

Columns from demo: Publication Date

Shortcut for supplying a standard strftime format to be applied to date and datetime objects.

This helper must be called inline with the column definition, in order to provide the format string. It will returned a stand-in callback to defer the formating until the callback is needed.

As of Django 1.5, support for localtime is available by passing in an extra keyword argument localize=True. Doing this on Django version 1.4 or earlier will raise an exception with a message detailing this fact.

datatable_options = {
    'columns': [
        # Standard use; must call the helper once to give it the format string
        ("Publication Date", 'pub_date', helpers.format_date("%m/%d/%Y"),
        # If Django 1.5 or later, you can ask for the localtime version
        ("Modified Date", 'mode_date', helpers.format_date("%Y-%m-%d", localize=True),
    ],
}

Using this helper in custom callbacks provides no tangible benefit over formatting the date or datetime instance yourself, but can still be done:

def get_column_Publication_Date_data(self, instance, *args, **kwargs):
    # Generate the real callback helper, then call it
    callback = helpers.format_date('%m/%d/%Y', localize=True)
    return callback(kwargs['default_value'])
    
    # This is equivalent real Python code, and better for readability
    from django.utils.timezone import localtime
    return localtime(kwargs['default_value']).strftime('%m/%d/%Y')