10

I have a field in my model

    published_on = models.DateTimeField() 

on a Django template page and I want to show only the date instead of date along with time. Any idea how to truncate the time form date in the django model form?

Thanks in advance.

2 Answers 2

9

Use the date filter in your template, for instance:

{{ form.published_on|date:"D d M Y" }}

Or just:

{{ form.published_on|date }}

You can customize the output the way you want, or use the locale default. See this link for details.

7
  • i am using django ModelForm and i want to truncate the value from {{form.published_on}}
    – burning
    Commented Feb 6, 2012 at 7:09
  • So simply apply the filter to that field, like {{form.published_on|date}}. I updated the answer.
    – pojo
    Commented Feb 6, 2012 at 7:12
  • How about {{ form.published_on.date|date }} ?
    – pojo
    Commented Feb 6, 2012 at 7:29
  • Perhaps you need to set a different input format for your field: published_on = models.DateTimeField(input_formats=["%d/%M"])
    – pojo
    Commented Feb 6, 2012 at 7:38
  • 2
    @pojo, I agree, the question isn't clear at all. You still can't use a date filter on a form field though! {{ form.published_on.value|date }} would work. Commented Feb 6, 2012 at 8:59
1

You could try to use SplitDateTimeWidget to represent the field in two widgets (https://docs.djangoproject.com/en/dev/ref/forms/widgets/#splitdatetimewidget). Then, for example, the time field could be hidden with CSS. This method is particularly useful if you need to preserve the actual time value, and just allow user to change the date.

Not the answer you're looking for? Browse other questions tagged or ask your own question.