简体   繁体   中英

pymongo datetime TypeError: an integer is required

I have the following code:

from datetime import datetime as dt
import time
import datetime

TODAY_DATE = datetime.date.today()
THREE_MONTH = datetime.timedelta(weeks=12)
FOUR_MONTH = datetime.timedelta(weeks=16)
START_DAY = TODAY_DATE - FOUR_MONTH
END_DAY = TODAY_DATE - THREE_MONTH 

x = agent_coll_obj.find({ "created_at": {"$gte" : datetime.datetime(START_DAY), 
                                             "$lt": datetime.datetime(END_DAY) }})
print x

I get the following error:

Traceback (most recent call last):
  File "main.py", line 212, in <module>
    program.runProgram()
  File "main.py", line 61, in runProgram
    report.RcreateReport()
  File "/Users/dwstein_old/Dropbox/hkpr_reports/report.py", line 105, in RcreateReport
    x = agent_coll_obj.find({ "created_at": {"$gte" : datetime.datetime(START_DAY), 
TypeError: an integer is required

I was trying to use this SO question as a guide. I'm trying to just use the whole date as opposed to having variables for the year, month, and day for each date.

from datetime import datetime as dt
import time
import datetime

TODAY_DATE = datetime.date.today()
THREE_MONTH = datetime.timedelta(weeks=12)
FOUR_MONTH = datetime.timedelta(weeks=16)
START_DAY = TODAY_DATE - FOUR_MONTH
END_DAY = TODAY_DATE - THREE_MONTH 

x = agent_coll_obj.find({ "created_at": {"$gte" : START_DAY, 
                                         "$lt": END_DAY }})
print x

you can use datetime.datetime like this:

In [2]: datetime.datetime?
Docstring:
datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])

The year, month and day arguments are required. tzinfo may be None, or an
instance of a tzinfo subclass. The remaining arguments may be ints or longs.
File:      /usr/local/opt/pyenv/versions/2.7.8/lib/python2.7/lib-dynload  /datetime.so
Type:      type

您需要为datetime.datetime函数指定年,月,日。

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