Filter model datetime Django

Asked

Viewed 23 times

0

I have a model that is basically the following :

class Sample(models.Model):
    date = fields.DateField(auto_now=True)

Now, with this I want to filter only the month in this model

How I only get the month on this model ?

1 answer

0

You can use the module datetime to do this.

Use the method .strftime(). See below:

from datetime import datetime

data_hora = datetime.now()
mes = data_hora.strftime('%m')
print(mes)

It will return only the month of the current time.

Completion

This way you transform your object datetime for an object str. It does this according to the formatting passed as argument to the method .strftime(). To get deeper into the formatting I recommend this reading.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.