Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何使用Python精确计算过去30天的时间(精确到分钟)?_Python_Datetime_Date_Time - Fatal编程技术网

我如何使用Python精确计算过去30天的时间(精确到分钟)?

我如何使用Python精确计算过去30天的时间(精确到分钟)?,python,datetime,date,time,Python,Datetime,Date,Time,在Python中,我试图检索正好是过去30天(30*24小时)的日期/时间。目前,我只是在做: >>> import datetime >>> start_date = datetime.date.today() + datetime.timedelta(-30) 它返回一个datetime对象,但不包含时间数据: >>> start_date.year 2009 >>> start_date.hour Traceback

在Python中,我试图检索正好是过去30天(30*24小时)的日期/时间。目前,我只是在做:

>>> import datetime
>>> start_date = datetime.date.today() + datetime.timedelta(-30)
它返回一个datetime对象,但不包含时间数据:

>>> start_date.year
2009
>>> start_date.hour
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'datetime.date' object has no attribute 'hour'
>>开始日期.year
2009
>>>开始日期。小时
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:'datetime.date'对象没有属性'hour'

您希望使用
日期时间
对象,而不仅仅是
日期
对象:

start_date = datetime.datetime.now() - datetime.timedelta(30)

date
仅存储日期和
time
仅存储时间
datetime
是一个带时间的日期。

不太清楚为什么有人-1'd this.以同样的方式工作,但我喜欢这样看待它
datetime.datetime.now()-datetime.timedelta(days=30)
因为显式优于隐式,所以我更喜欢这种方式。