How to Extract the Year from a DateTimeField in Django



Python


In this article, we show how to extract the year from a DateTimeField in Django.

So when we create a DateTimeField in Django, this creates a date and a time for any row. The date has a day, month, and year. The time has a hour, minute, and seconds.

Now there are times when we want to retrieve a single attribute from either the date or the time.

For example, let's say we're running a site where a user can post content on the site. We may not want to put the entire datetimefield object, where it has the day, month, year and time. We may just want to put the day, month, and year.

Let's say, for example, we just want to extract the year from the DateTimeField.

We will show how to do this now in the Django web framework.

And most of the code functionality goes all in the models.py file.

This is shown below.



So we have our models.py file.

We create a model called Post.

This model represents all the information about a post that a user submits.

The field we're interested in is the pub_date field. This represents the DateTimeField object of the time when the post was published. Being a DateTimeField, it contains the date and the time the post was published.

But many times, as discussed before, we don't want the entire DateTimeField object. We may just want the day or the month, or the day and the month and the year, and not the time. We can customize this however we feel fit.

In this example, we just want to extract the year from the DateTimeField object.

So, in order to extract the year from a DateTimeField in Django, we really just use plain Python code.

Python has identifiers for various elements of datetime objects.

To extract the year, we use the strftime() function on a DateTimeField object and use the format identifier, %Y, which represents the year.

Below is a full table representing the different format identifiers for date and time objects in Python.

Directive Meaning
%a Weekday's abbreviated name
%A Weekday's full name
%w Weekday as a decimal number; 0= Sunday and 6=Saturday
%d Day of the month as a zero-padded decimal number
%b Month's abbreviated name
%B Month's full name
%m Month as a zero-padded decimal number
%y Year without centry as a zero-padded decimal number
%Y Year with century as a decimal number
%H Hour (24-hour time) as a zero-padded decimal number
%I Hour (12-hour time) as a zero-padded decimal number
%p Locale's equivalent of either AM or PM
%M Minute as a zero-padded decimal number
%S Second as a zero-padded decimal number
%f Microsecond as a decimal number, zero-padded on the left
%z UTC offset in the form +HHMM or -HHMM
%Z Time zone name (empty string if the object is naive)
$j Day of the year as a zero-padded decimal number


Now this function above has extracted the year.

Now to show this in an actual template file, we use the database object followed by a dot and yearpublished.

views.py File

Below is the views.py file for a detail view.





The reason I showed this detail_post_view function is becasue it shows you how the postobj is an object of the Post model.

It's a detail view of an object in the post model.

This postobj is then passed into the template file through the context dictionary.

Now we go to the template file to show how to show which year this post was created.

Template file

So in the template file to show which year the post was created, we specify the following shown below.



And this code shows the year the post was published. So this is all we have to do to extract the year from a DateTimeField in Django . m

Again, you can customize the function to show anything such as only the month, only the day, the day, month, and year, etc.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...