Quepinch

How to extract year, month, day from date in Django

Sep 1, 2021

You can use Extract class for extracting various components from date as number

from django.db.models.functions import Extract

Following are the lookup names to extract different components.

Given the datetime 2015-06-15 23:30:01.000321+00:00, the built-in lookup_names return:

  • “year”: 2015
  • “iso_year”: 2015
  • “quarter”: 2
  • “month”: 6
  • “day”: 15
  • “week”: 25
  • “week_day”: 2
  • “iso_week_day”: 1
  • “hour”: 23
  • “minute”: 30
  • “second”: 1

Read it in more detail on Django official website: https://docs.djangoproject.com/en/3.2/ref/models/database-functions/

Author
Ankit Shah

I write simple articles that may help Django developers build web apps