How can I check if a date is the same day as datetime.today()?

Python

Python Problem Overview


This condition always evaluates to True even if it's the same day because it is comparing time.

from datetime import datetime

# ...

if date_num_posts < datetime.today(): 

How can I check if a date is the same day as datetime.today()?

Python Solutions


Solution 1 - Python

If you want to just compare dates,

yourdatetime.date() < datetime.today().date()

Or, obviously,

yourdatetime.date() == datetime.today().date()

If you want to check that they're the same date.

The documentation is usually helpful. It is also usually the first google result for python thing_i_have_a_question_about. Unless your question is about a function/module named "snake".

Basically, the datetime module has three types for storing a point in time:

  • date for year, month, day of month
  • time for hours, minutes, seconds, microseconds, time zone info
  • datetime combines date and time. It has the methods date() and time() to get the corresponding date and time objects, and there's a handy combine function to combine date and time into a datetime.

Solution 2 - Python

  • If you need to compare only day of month value than you can use the following code:

     if yourdate.day == datetime.today().day:
         # do something
    
  • If you need to check that the difference between two dates is acceptable then you can use timedelta:

     if (datetime.today() - yourdate).days == 0:
         #do something
    
  • And if you want to compare date part only than you can simply use:

     from datetime import datetime, date
     if yourdatetime.date() < datetime.today().date()
         # do something
    

Note that timedelta has the following format:

datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])

So you are able to check diff in days, seconds, msec, minutes and so on depending on what you really need:

from datetime import datetime
if (datetime.today() - yourdate).days == 0:
    #do something

In your case when you need to check that two dates are exactly the same you can use timedelta(0):

from datetime import datetime, timedelta
if (datetime.today() - yourdate) == timedelta(0):
    #do something

Solution 3 - Python

You can set the hours, minutes, seconds and microseconds to whatever you like

datetime.datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)

but trutheality's answer is probably best when they are all to be zero and you can just compare the .date()s of the times

Maybe it is faster though if you have to compare hundreds of datetimes because you only need to do the replace() once vs hundreds of calls to date()

Solution 4 - Python

all(getattr(someTime,x)==getattr(today(),x) for x in ['year','month','day'])

One should compare using .date(), but I leave this method as an example in case one wanted to, for example, compare things by month or by minute, etc.

Solution 5 - Python

Another alternative is to convert the date in strings and compare them.

from datetime import datetime

today= datetime.today()

if today.strftime('%Y-%m-%d') == other_datetime.strftime('%Y-%m-%d'):
    print("same day as today: {0}".format(today.strftime('%Y-%m-%d')))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmadpropsView Question on Stackoverflow
Solution 1 - PythontruthealityView Answer on Stackoverflow
Solution 2 - PythonArtsiom RudzenkaView Answer on Stackoverflow
Solution 3 - PythonJohn La RooyView Answer on Stackoverflow
Solution 4 - PythonninjageckoView Answer on Stackoverflow
Solution 5 - PythonPapouche GuinslyzinhoView Answer on Stackoverflow