QDate
It is the core class for processing dates in PyQt5, used to represent Gregorian dates (year, month, day), and supports date calculation, formatting and validity verification. Suitable for date input, data filtering, schedule management and other functions in GUI development.
Core functions
- Date creation: Supports year, month, day or string parsing initialization.
- Date calculation: add or subtract days/month/years, get the date difference.
- Format output: Convert to string (supports localized format).
- Efficiency check: Automatically verify the legality of dates (such as leap years, month range).
- Static method: Quickly get the current date or a specific date.
Common methods and code examples
1. Create a date object
method | illustrate | Example |
---|---|---|
Constructor | Created through year, month, day | QDate(year, month, day) |
fromString(date_str, format) | Parsing the string as a date | ("2023-10-01", "yyyy-MM-dd") |
currentDate() | Get the current date (static method) | () |
from import QDate # Create a date objectdate1 = QDate(2023, 10, 1) # October 1, 2023date2 = ("2023/12/31", "yyyy/MM/dd") # parse from stringtoday = () # Current date
2. Obtain date information
method | illustrate | Example |
---|---|---|
year() | Return to year | () → 2023 |
month() | Return to month (1~12) | () → 10 |
day() | Return the number of days (1~31) | () → 1 |
dayOfWeek() | Return to the day of the week (1=Monday, 7=Sunday) | () → 7 |
daysInMonth() | Return to the total number of days in the month | () → 31 |
print(f"{('yyyy-MM-dd')} It's week{()}") # Output:2023-10-05 It's week4(Thursday)
3. Date calculation and comparison
method | illustrate | Example |
---|---|---|
addDays(days) | Increase the specified number of days | (7) → One week later |
addMonths(months) | Increase the specified number of months | (2) → 2023-12-01 |
addYears(years) | Increase the number of years specified | (1) → 2024-10-01 |
daysTo(date) | Calculate the number of days between two days | (date2) → 91 |
operator (<, >, ==) | Compare dates | date1 < date2 → True |
# Calculate future datesfuture_date = (3) # Date after 3 months # Calculate the date differencedays_remaining = (QDate(2023, 12, 31)) # Days to the end of the year
4. Date formatting and conversion
method | illustrate | Example |
---|---|---|
toString(format) | Convert to string by format | ("yyyyy's M-d-Day") → "October 1, 2023" |
toPyDate() | Convert to Python object | datetime_date = () |
Common formats:
-
yyyy
: 4-digit year (such as 2023) -
MM
: Two months (01~12) -
dd
: Two-digit days (01~31) -
ddd
: Localized week abbreviation (such as "Monday") -
dddd
: Localized full name of the week (such as "Monday")
print(("yyyy-MM-dd")) # 2023-10-01 print(("dd/MM/yyyy")) # 01/10/2023 print(("MMMM d, yyyy")) # October 1, 2023(Need localization support)
5. Date validity verification
method | illustrate | Example |
---|---|---|
isValid() | Check if the date is legal | QDate(2023, 2, 30).isValid() → False |
isNull() | Check if the date is empty (default construct) | QDate().isNull() → True |
# Verify the user input dateuser_input = QDate(2023, 13, 1) #Invalid monthif user_input.isValid(): print("Date valid") else: print("Invalid date")
Full example: Date calculator
from import QDate def date_calculator(): today = () print(f"Current date: {('yyyy-MM-dd')}") # Calculate the date after 30 days future_date = (30) print(f"30The Queen of Heaven: {future_date.toString('yyyy-MM-dd')}") # Calculate the number of days between two dates deadline = QDate(2023, 12, 31) days_left = (deadline) print(f"There is still a year-end {days_left} sky") # Verification date test_date = QDate(2024, 2, 29) # 2024 is a leap year print(f"2024-02-29 Is it valid or not? {test_date.isValid()}") # True date_calculator()
Things to note
-
Moon and days range:
- Month range: 1~12
- Day range: Automatic verification based on the month (such as April cannot exceed 30 days).
-
With Python
datetime
Interaction:- use
toPyDate()
Convert toObjects for interaction with other libraries.
- from
Conversion:
import datetime py_date = (2023, 10, 1) qt_date = QDate(py_date.year, py_date.month, py_date.day)
- use
-
Localization format:
- The formatted output will be automatically adapted according to the system language (such as "October 1, 2023" in the Chinese environment).
ByQDate
, can efficiently process date logic, combined withQDateEdit
The control can build a user-friendly date input interface!
This is the end of this article about the specific use of the PyQt5 QDate class. For more related content of the PyQt5 QDate class, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!