Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django DateTimeField

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.

like image 378
burning Avatar asked Feb 06 '12 06:02

burning


People also ask

What is DateTimeField in Django?

DateTimeField is a date and time field which stores date, represented in Python by a datetime. datetime instance. As the name suggests, this field is used to store an object of datetime created in python. The default form widget for this field is a TextInput .

How do I get the current date and time in Django?

today() method to get the current local date. By the way, date. today() returns a date object, which is assigned to the today variable in the above program. Now, you can use the strftime() method to create a string representing date in different formats.

How is Django utils time zone now?

The solution to this problem is to use UTC in the code and use local time only when interacting with end users. Time zone support is disabled by default. To enable it, set USE_TZ = True in your settings file. In Django 5.0, time zone support will be enabled by default.

What is timestamped model in Django?

TimeStampedModel - An Abstract Base Class model that provides self-managed created and modified fields.


2 Answers

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.

like image 65
pojo Avatar answered Sep 19 '22 12:09

pojo


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.

like image 20
Raekkeri Avatar answered Sep 17 '22 12:09

Raekkeri