Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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()?

like image 855
madprops Avatar asked Jun 20 '11 06:06

madprops


People also ask

How do you know if two dates are the same day?

To check if two dates are the same day, call the toDateString() method on both Date() objects and compare the results. If the output from calling the method is the same, the dates are the same day.

How does Python compare datetime today?

You can use equal to comparison operator = to check if one datetime object is has same value as other. In the following program, we initialize two datetime objects, and then check if both datetime objects have same date and time.

How do you compare datetime dates?

Use the datetime Module and the < / > Operator to Compare Two Dates in Python. datetime and simple comparison operators < or > can be used to compare two dates. The datetime module provides the timedelta method to manipulate dates and times.


1 Answers

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.
like image 104
trutheality Avatar answered Sep 28 '22 14:09

trutheality