Evlikosh Dawark
Evlikosh Dawark

Reputation: 640

How to subtract n number of days from a given date in python

I have implemented the database as follows

from google.appengine.ext import db
from google.appengine.ext import blobstore

class Medicine:
 id =  db.IntegerProperty(required=True)
 exp_date = db.DateProperty()

and in entity manager i want to get all the medicines where expiry date - 20

MedicineEntity:
 def get_medicines(self):
 query = db.GqlQuery("SELECT * FROM Medicine WHERE exp_date=:1", exp_date -20 )

I know that my query is wrong . is there a way to get the difference like sql in Gql. Can any one help?

Upvotes: 0

Views: 652

Answers (1)

Shawabawa
Shawabawa

Reputation: 2597

The standard way of doing this in python is using timedelta.

eg:

from datetime import datetime, timedelta
now = datetime.now()
delta = timedelta(days=20)

then = now - delta

So 1 option would be to convert exp_date into a datetime, subtract the timedelta and then convert it back to a dateproperty.

I haven't used appengine so I don't know how hard that would be exactly.

edit: Some searching shows that DateProperty actually stores the value as a datetime object, so it should be as simple as

query = db.GqlQuery("SELECT * FROM Medicine WHERE exp_date=:1", exp_date - timedelta(days=20) )

Upvotes: 6

Related Questions