6

I have a DateField in django whose default value is set to timezone.now

How can I get the week of the day. I mean the day is either sunday or monday or other ??

varad
  • 7,309
  • 20
  • 60
  • 112

2 Answers2

9

A Django DateField is

represented in Python by a datetime.date instance

So in Python code you can use date.weekday() or date.isoweekday() on it.

In a template you should use the date filter, e.g.

Today is {{ date_variable|date:"l" }}
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
1

You can use a small piece of code to do that:

import datetime
from django.db import models

def get_monday():
    today = datetime.datetime.now()
    return today - datetime.timedelta(today.weekday())

class MyModel(models.Model):
    date = models.DateField(default=get_monday)

also sunday:

def get_sunday():
    today = datetime.datetime.now()
    return today + datetime.timedelta(7 - today.weekday() - 1)
Allen Shaw
  • 1,164
  • 7
  • 23