How can I compare a date and a datetime in Python?

后端 未结 5 1244
野趣味
野趣味 2020-12-01 03:12

Here\'s a little snippet that I\'m trying execute:

>>> from datetime import *
>>> item_date = datetime.strptime(\'7/16/10\', \"%m/%d/%y\")
         


        
相关标签:
5条回答
  • 2020-12-01 03:51

    In my case, I get two objects in and I don't know if it's date or timedate objects. Converting to date won't be good as I'd be dropping information - two timedate objects with the same date should be sorted correctly. I'm OK with the dates being sorted before the datetime with same date.

    I think I will use strftime before comparing:

    >>> foo=datetime.date(2015,1,10)
    >>> bar=datetime.datetime(2015,2,11,15,00)
    >>> foo.strftime('%F%H%M%S') > bar.strftime('%F%H%M%S')
    False
    >>> foo.strftime('%F%H%M%S') < bar.strftime('%F%H%M%S')
    True
    

    Not elegant, but should work out. I think it would be better if Python wouldn't raise the error, I see no reasons why a datetime shouldn't be comparable with a date. This behaviour is consistent in python2 and python3.

    0 讨论(0)
  • 2020-12-01 04:06

    Here is another take, "stolen" from a comment at can't compare datetime.datetime to datetime.date ... convert the date to a datetime using this construct:

    datetime.datetime(d.year, d.month, d.day)
    

    Suggestion:

    from datetime import datetime
    
    def ensure_datetime(d):
        """
        Takes a date or a datetime as input, outputs a datetime
        """
        if isinstance(d, datetime):
            return d
        return datetime.datetime(d.year, d.month, d.day)
    
    def datetime_cmp(d1, d2):
        """
        Compares two timestamps.  Tolerates dates.
        """
        return cmp(ensure_datetime(d1), ensure_datetime(d2))
    
    0 讨论(0)
  • 2020-12-01 04:11

    Create and similar object for comparison works too ex:

    from datetime import datetime, date
    
    now = datetime.now()
    today = date.today()
    
    # compare now with today
    two_month_earlier = date(now.year, now.month - 2, now.day)
    if two_month_earlier > today:
        print(True)
    
    two_month_earlier = datetime(now.year, now.month - 2, now.day)
    if two_month_earlier > now:
       print("this will work with datetime too")
    
    0 讨论(0)
  • 2020-12-01 04:12

    I am trying to compare date which are in string format like '20110930'

    benchMark = datetime.datetime.strptime('20110701', "%Y%m%d") 
    
    actualDate = datetime.datetime.strptime('20110930', "%Y%m%d")
    
    if actualDate.date() < benchMark.date():
        print True
    
    0 讨论(0)
  • 2020-12-01 04:16

    Use the .date() method to convert a datetime to a date:

    if item_date.date() > from_date:
    

    Alternatively, you could use datetime.today() instead of date.today(). You could use

    from_date = from_date.replace(hour=0, minute=0, second=0, microsecond=0)
    

    to eliminate the time part afterwards.

    0 讨论(0)
提交回复
热议问题