0

I'm writing a blog for my portfolio and I wanna compare (in the Django template) the date I published an article with the date I edited it in order to display the "Edit date" only if it was edited before.

The problem is: I don't wanna test for every single date field, like year, month, day, hour, minute, etc., but if I simply compare the two values, it'll always display both values, since the time precision goes beyond seconds, making the dates different even though I never edited that particular post.

TL;DR How to compare dates when their values will always differ, given the precision Django takes time, without using an iffor every value (year, month, day, hour, minute, second)?

What I tried: I successfully accomplished solving my main problem, but my code looks something like if this == that and if this == that etc.. It doesn't look pretty and I don't wanna code this for every website I build. It looks like there's a better way to solve it.

Daniel
  • 21
  • 5
  • Simplest way would be to make sure the "Last edited" field is null for brand new articles. Is there a reason you can't do that? – John Gordon Apr 08 '23 at 19:45
  • Please provide enough code so others can better understand or reproduce the problem. – Community Apr 08 '23 at 20:43
  • Is it possible to show your current modle and template (well not per se the entire template, just the aspects that are vital to reproduce it? – Willem Van Onsem Apr 08 '23 at 21:38

1 Answers1

0

for comparison you can check the diff between two datetime objects

(edited-created).total_seconds()

as suggested by John Gordon you should just just leave it empty till you actually have edited the post

David Wenzel
  • 251
  • 1
  • 7