site logo

Stacknatic

Stacknatic logo

We Care About Your Privacy

Stacknatic utilizes technologies, such as cookies, to enhance your browsing experience. By using this technology, you can be provided with a more personalized and seamless interaction with this website. By continuing, you agree with the Privacy Policy of Stacknatic.

Privacy Policy | Terms of Use
Home/blog/How to compare two dates on a Django site

How to compare two dates on a Django site

featured image for How to compare two dates on a Django site

How to compare two dates on a Django site

Published on: January 6, 2023 (Updated on: July 14, 2024)

Table of Contents

  • Step 1
  • Step 2
  • What the above code does:

Sometimes you might want to compare two dates on a Django site or blog in other to show the last updated date or time. There are several ways to do this but this post shows you how to compare two dates in Django template with just a few lines of code.

Step 1

If you have not created a date and last updated time in your Django database, add the following in your relevant models.py:

published = models.DateTimeField(auto_now_add=True) last_update = models.DateTimeField(auto_now=True) 

The first line of code (published) will add the date your post was published and the second line of code (last_update) will add the last time your post was updated.

Step 2

To compare dates with python on your Django site, add the following code in your Django template:

{% if post.last_update|date:"dmYHis" > post.published|date:"dmYHis" %} | Updated on {{ post.last_update|date }} {% endif %} 

What the above code does:

The code basically converts both dates to numerical values and compares them. If post.last_update is greater than post.published, the last updated date will be displayed on your Django site or blog.

See more posts in Websites
Author:author's avatarMichael

Recommended Posts

featured image for How to Create a Django Web App (with Custom User Model)

How to Create a Django Web App (with Custom User Model)

Learn how to create a Django web app with a custom user model, covering setup and the essential steps to tailor your application to your needs.

featured image for CSRF Attack and Implications Explained in Simple Terms With Example

CSRF Attack and Implications Explained in Simple Terms With Example

An explanation of Cross-Site Request Forgery (CSRF) attack, its implications, and effective strategies to protect web applications from unauthorized actions.

featured image for How to Trap Focus in Next.js and React

How to Trap Focus in Next.js and React

Trapping focus ensures that keyboard users can navigate your component without losing focus elsewhere on the page. Learn how to trap focus in React and Next.js.

featured image for How to Implement Debouncing in Next.js

How to Implement Debouncing in Next.js

Debouncing can be used to prevent performance issues or data inaccuracies that may arise from multiple component renderings or repetitive user actions.

featured image for Mutable vs Immutable Data in JavaScript and React.js

Mutable vs Immutable Data in JavaScript and React.js

In programming, data structures can generally be classified as either mutable or immutable. Here is a simplified explanation of both in JavaScript and React.js.