Table of Contents
Use Comparison Operator with now()
To check if the specified date is greater than today in Python:
- Use
.datetime.now()
to get the current local time and date. - Create a
datetime
object usingdatetime.datetime()
with the specified date and time. - Use the greater than(
>
) operator withif-else
to assess if the given date is greater than today.
1 2 3 4 5 6 7 8 9 10 11 |
import datetime today = datetime.datetime.now() date = datetime.datetime(2023, 5, 13, 22, 50, 55) if date > today: print(True) else: print(False) |
The above code will show us the following output.
1 2 3 |
False |
First, let’s understand what we mean by checking if a date is greater than today.
For instance, if we have a current date/time and some date/time from the future then the future’s date/time would be greater than the current date/time.
Similarly, the current date/time would be greater than the date/time of any day from the past.
Next, we imported the datetime
module to work with date and time while checking if the specified date is greater than today or not but if your project requires date
only then you are required to import the date
class as from datetime import date
.
After that, we used the datetime.datetime.now()
function to have the current local time and date. Mostly, learners get confused to see datetime
twice while using the now()
function but it’s very simple.
Here, the first datetime
is the module, the second datetime
is the class and now()
is a function of the datetime
class which belongs to the datetime
module.
Then, we instantiated the datetime
class by specifying a future’s date/time. Lastly, we used the if-else
statement which printed True
if the given date/time is greater than today; otherwise False
.
Now suppose, there might be a situation where we have to compare the given date/time with a different time zone’s current date/time. Let’s see how we can do that.
Compare the datetime
of Different Time Zones
To examine if the date is greater than the different time zone’s today in Python:
- Use
.datetime.now()
by specifying a different time zone, we are using Pacific Standard Time here. - Instantiate
datetime
by passing date and time. - Use
localize()
to makenaive
time zoneaware
. - Use the greater than(
>
) operator withif-else
to compare two dates and print the results accordingly,True
if the condition satisfies; otherwiseFalse
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import datetime import pytz #Pacific Standard Time is used (UTC−08:00) today = datetime.datetime.now(datetime .timezone(datetime .timedelta(hours=-8))) date = datetime.datetime(2022, 11, 27, 22, 20, 55) date = pytz.utc.localize(date) if date > today: print(True) else: print(False) |
The above code fence gives us the following output on successful execution.
1 2 3 |
False |
The above code’s logic is similar to the one we implemented in the previous code block but we used the pytz
library. Note that, you must have pytz
installed on your machine.
The above program got the current date according to Pacific Standard Time. Here, datetime.timezone()
implemented the tzinfo
which was set to None
in the previous example and that’s its default behaviour.
While datetime.timedelta()
returned the duration which denotes the difference between two datetime
, time
or date
objects to the microsecond resolution.
Next, we created a datetime
object which contains the local date and time.
Remember that the datetime
instance is naive
by default so, we had to make both objects either aware datetime
or naive datetime
; otherwise, the code will generate TypeError
saying can not compare offset-aware and offset-naive datetimes
.
To avoid this error, we used the localize()
method of the utc
class which belongs to the pytz
module. The utc.localize()
is used to make a naive
time zone aware
.
Now the point is, how will we know which variable is naive
or aware
? An aware
datetime
has an associated time zone while naive
doesn’t. So, the variable today
is the aware datetime
and date
is the naive datetime
.
Finally, we used if-else
to compare both dates which resulted in True
if date > today
fulfils; otherwise, False
.
Till this point, we were given date/time in numbers but assume that the project requires to get the date as string input in a specific format. For instance, we want to have dates as dd/mm/YYYY format.
Let’s learn how we can convert that string input to a datetime
object and compare them.
Further reading:
Use Comparison Operator with strptime()
To look over if the date is greater than today in Python:
- Use
strptime()
to specify a date as a string type value in thedd/mm/YYYY
format. - Use the
now()
function to get the current local time and date. - Use the greater than(
>
) operator withif-else
to compare where the date is greater than today or not and printTrue
orFalse
accordingly.
1 2 3 4 5 6 7 8 9 10 11 |
import datetime date = datetime.datetime.strptime("25/10/2023", "%d/%m/%Y") today = datetime.datetime.now() if date > today: print(True) else: print(False) |
It will give us the following outcome.
1 2 3 |
True |
In this code block, we used the strptime()
method of the datetime
class which belongs to the datetime
module. It took two parameters; the first is the date in string format and the second is the format in which we want to represent the given date.
Further, the strptime()
converts this specific format of string type date to a datetime
object. After that, we get the current local date/time using .datetime.now()
.
Finally, the if-else
statement is used to compare both results and display True
/False
based on the if
condition. We’ll get True
if the condition is satisfied; otherwise, False
.