datetime
strptime
We have shared before :Python Get a date that is “ What day ” Of 6 Methods ! actually , When we use Python Processing date / In time , We often encounter all kinds of problems . Today we will discuss another problem , How to use Python Compare two dates ?
datetimeIf you need to use Python Processing date and time , You will definitely think of datetime、time、calendar Equal module . In the Middle East: ,datetime The module is mainly used to represent the date and time , It's what we often call date / Minutes and seconds .
datetime Common classes in modules :
Class name function description date Date object , Common attributes are year,month,daytime Time object datetime Date time object , Common attributes are hour,minute,second,microsecondtimedelta The time interval , The length between two time points tzinfo Time zone information objectthat , How to use datetime The module compares two dates ?
In the interactive environment, enter the following command :
import datetimefirst_date = datetime.date(2022, 2, 22)second_date = datetime.date(2022, 3, 1)print(first_date < second_date)
Output :
True
We will find that datetime
Modules can use comparison operators <
or >
To compare two dates . The code above compares date objects , If you change to date and time objects, you can also compare them in this way .
In the interactive environment, enter the following command :
import datetimefirst_date = datetime.datetime(2022, 2, 22, 12, 5, 0)second_date = datetime.datetime(2022, 3, 1, 12, 5, 0)print(first_date < second_date)
Output :
strptimeTrue
In the previous example code , In fact, the comparisons are all date objects / Date time object . But if the user enters 、 Or the date and time of batch import are in string format , The first step in our comparison is First the str Convert to datetime.
The conversion method is also very simple , Just go through datetime.strptime
That is to say .
In the interactive environment, enter the following command :
import datetimestrftime1 = datetime.datetime.strptime("2022-02-22", "%Y-%m-%d")strftime2 = datetime.datetime.strptime("2022-03-01", "%Y-%m-%d")print(" date 2022-02-22 Greater than 2022-03-01:", strftime1 > strftime2)
Output results :
in addition time Also in the module strptime()
function , The time string can be parsed into time tuples according to the specified format , Using this feature, you can also compare two dates .
In the interactive environment, enter the following command :
import timestrftime1 = time.strptime("2022-02-22", "%Y-%m-%d")strftime2 = time.strptime("2022-03-01", "%Y-%m-%d")print(strftime1)print(strftime2)print(" date 2022-02-22 Greater than 2022-03-01:", strftime1 > strftime2)
Output results :
above , Is how to use Python Several small ways to compare two dates . actually ,Python Different modules of time processing in 、 There are many different functions that can be summarized .
calendar( The calendar ) modular 、time( Time ) In the module, we will introduce their small knowledge points in detail later .
This is about Python This is the end of the detailed article comparing the two methods of two dates , More about Python Please search the previous articles of SDN or continue to browse the relevant articles below. I hope you will support SDN more in the future !