Skip to content

Instantly share code, notes, and snippets.

@marcusshepp
Created June 18, 2016 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcusshepp/1e7e990dc0753719e7c2f01819852b2e to your computer and use it in GitHub Desktop.
Save marcusshepp/1e7e990dc0753719e7c2f01819852b2e to your computer and use it in GitHub Desktop.
time since object creation
class DateCreatedAndUpdate(models.Model):
"""
Abstract class used to populate childern with a `date_created` field
"""
class Meta:
abstract, ordering = True, ("-id",)
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
def time_past_since_creation(self):
time = (timezone.now()-self.date_created)
hours = time.seconds / 60 / 60
weeks = time.days / 7
return "Weeks: {weeks}, Days: {days}, Hours: {hours}, Seconds: {seconds}".format(
days=time.days, seconds=time.seconds, weeks=weeks, hours=hours
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment