Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีเปรียบเทียบ Date Strings ใน Python

Python Date Implementations รองรับ Comparison Operators ทั้งหมด หากใช้ datetime Module สร้างและจัดการ Date Objects สามารถใช้ <, >, <=, >= กับ Dates ได้โดยตรง ทำให้ Validate และ Compare Dates ง่ายมาก

ตัวอย่าง

from datetime import datetime, timedelta

today = datetime.today()
yesterday = today - timedelta(days=1)

print(today < yesterday)   # False
print(today > yesterday)   # True
print(today == yesterday)  # False

ผลลัพธ์

False
True
False