程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Detailed explanation of two methods for Python to compare two dates

編輯:Python

Catalog

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 ?

datetime

If 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 object

that , 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 :

True

strptime

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 !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved