简体   繁体   English

无法将 datetime.datetime 与 datetime.date 进行比较

[英]can't compare datetime.datetime to datetime.date

I have the following code and am getting the above error.我有以下代码并收到上述错误。 Since I'm new to python I'm having trouble understanding the syntax here and how I can fix the error:由于我是 python 的新手,因此无法理解此处的语法以及如何修复错误:

if not start or date < start: start = date

There is a datetime.date() method for converting from a datetime to a date.有一个datetime.date()方法可以将日期时间转换为日期。

To do the opposite conversion, you could use this function datetime.datetime(d.year, d.month, d.day)要进行相反的转换,您可以使用此函数datetime.datetime(d.year, d.month, d.day)

You can use the datetime.datetime.combine method to compare the date object to datetime object, then compare the converted object with the other datetime object.您可以使用datetime.datetime.combine方法将日期对象与日期时间对象进行比较,然后将转换后的对象与其他日期时间对象进行比较。

import datetime

dt1 = datetime.datetime(2011, 03, 03, 11, 12)
day = datetime.date(2011, 03, 02)
dt2 = datetime.datetime.combine(day, datetime.time(0, 0))

print dt1 > dt2

Assuming start is a datetime, Use it like this:假设开始是一个日期时间,像这样使用它:

if not start or date < start.date(): start = date

I don't think there is a need to convert date to datetime in python, as you can just do the opposite and compare.认为不需要在 python 中将日期转换为日期时间,因为您可以做相反的事情并进行比较。

Or else you have other methods to create a new datetime by using the date to convert and time at 00:00.或者,您还有其他方法可以通过使用要转换的日期和 00:00 的时间来创建新的日期时间。

I was receiving the above error while using pandas , however, because the date_column was the string I wasted a lot of time without realizing I was formatting the wrong thing:但是,我在使用pandas时收到了上述错误,因为date_column是我浪费了很多时间而没有意识到我格式化错误的字符串:

# didnt work
df[(df.date_column > parse_datestr('2018-01-01'))]

# works
df['date_column'] = pd.to_datetime(df['date_column'])
df[(df.date_column > '2018-01-01') & (df.date_column < '2018-02-28')]

This problem arises when you are trying to compare a date field ( DateField ) and a datetime field ( DateTimeField ).当您尝试比较日期字段 ( DateField ) 和日期时间字段 ( DateTimeField ) 时,会出现此问题。

The solution would be check where you defined the fields in your models and ensure that the types are uniform.解决方案是检查您在模型中定义字段的位置并确保类型是统一的。

I would suggest you replace all DateField with DateTimeField .我建议你DateTimeField替换所有DateField

Your variables start and date are of different type I guess.我猜你的变量 start 和 date 是不同的类型。 One is a datetime and one is a date.一个是日期时间,一个是日期。 You may have to show more code in order to get decent help.您可能需要显示更多代码才能获得体面的帮助。

But look at this: http://docs.python.org/library/datetime.html#available-types但看看这个: http : //docs.python.org/library/datetime.html#available-types

It tells you that datetime.datetime has attributes like day, month and year, just like datetime.date.它告诉您 datetime.datetime 具有诸如日、月和年之类的属性,就像 datetime.date 一样。

Wow, question and answers are too old, it needs update.哇,问题和答案太旧了,需要更新。 Converting datetime.datetime object to datetime.date object is just easy:将 datetime.datetime 对象转换为 datetime.date 对象很简单:

somestringtext = '7.04.2021'
datetime_datetime_object = datetime.strptime(somestringtext, '%d.%m.%Y')
### returns datetime.datetime(2021, 4, 7, 0, 0)
datetime_date_object = datetime.date(datetime_datetime_object)

And datetime object is not same as date object, you cant compare而且 datetime 对象与 date 对象不同,您无法比较

datetime_datetime_object == datetime_date_object
### returns False

unless you convert them to same format:除非您将它们转换为相同的格式:

datetime.date(datetime_datetime_object) == datetime_date_object
### returns True

I solved it using.date() function available with datetime object.我使用日期时间 object 解决了它。 Here is how:方法如下:

date_object = datetime_object.date()

and then you can compare it with any datetime.date object.然后您可以将其与任何 datetime.date object 进行比较。 Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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