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 ??
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" }}
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With