How to Get Year from Datetime field in Django?

Hi Dev,
This tutorial will provide example of how to get year from datetime field in django. I explained simply step by step how to get year from datetime field in django. you'll learn How to extract year from datetime field in django. we will help you to give example of how to get year from datetime in django. So, let's follow few step to create example of how to get only year from datetime field in django.
For in this example, let's talk about 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 extractYear, We may just want to put the day.
So, let's say, for example, we just want to extract the year name from the DateTimeFieldin django.
let's see bellow example here you will learn how to get year from datetime field in django.
Django Admin Interface:

Step 1: Creating the Model
In this step we will require the database model for storing article details in article table.Open the models.py file and add the following code:
models.pyfrom django.db import models from django.contrib.auth.models import User from django.template.defaultfilters import slugify class Article(models.Model): title= models.CharField(max_length=300) url= models.SlugField(max_length=300) content= models.TextField() pub_date = models.DateTimeField(auto_now_add= True) author= models.ForeignKey(User,on_delete=models.CASCADE) def save(self, *args, **kwargs): self.url= slugify(self.title) super(Article, self).save(*args, **kwargs) def __str__(self): return self.title #Get Day Name def getyearname(self): return self.pub_date.strftime('%Y')Step 2: Creating the Views
In this step, we need to configure our views. The detail_post_view page will just be a template, open the views.py file and add:
models.pyfrom django.shortcuts import render, get_object_or_404,redirect from .forms import * from .models import * def detail_post_view(request, id=None): postobj= Article.objects.all() context={'postobj': postobj} return render (request, 'detail.html', context)Step 3: Creating the Template
Next, open the detail.html file and the add:
detail.html<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Tuts-Station.com</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script> <style type="text/css"> body{ background-color: #f7fcff; } </style> </head> <body> <div class="container mt-5 pt-5"> <div class="row d-flex justify-content-center"> <div class="col-md-9"> <div class="card"> <div class="card-header"> <h4>How to Get Year from Datetime field in Django? - <span class="text-primary">Tuts-Station.com</span></h4> </div> <div class="card-body"> <table class="table table-bordered"> <thead> <tr> <th>Title</th> <th>Publish Day</th> </tr> </thead> <tbody> {% for post in postobj %} <tr> <td>{{ post.title }}</td> <td class="text-primary">{{ post.getyearname }}</td> </tr> {% endfor %} </tbody> </table> </div> </div> </div> </div> </div> </body> </html>
I Hope It will help you....
Happy Python Coding!