简体   繁体   中英

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?

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) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM