from PyQt5.QtCore import QDateTime # Create a QDateTime object from a string date_str = "2021-09-01 14:30:00" dt = QDateTime.fromString(date_str, "yyyy-MM-dd HH:mm:ss") # Print the date and time values print("Year:", dt.date().year()) print("Month:", dt.date().month()) print("Day:", dt.date().day()) print("Hour:", dt.time().hour()) print("Minute:", dt.time().minute()) print("Second:", dt.time().second())
from PyQt5.QtCore import QDateTime # Create a QDateTime object dt = QDateTime.currentDateTime() # Format the QDateTime object as a string date_str = dt.toString("yyyy-MM-dd HH:mm:ss") # Print the string value print(date_str)This example shows how to format a QDateTime object as a string using the QDateTime.toString() method. The format string "yyyy-MM-dd HH:mm:ss" specifies the date and time values to include in the resulting string. Package Library: PyQt5 Overall, PyQt5's QtCore QDateTime class is a powerful tool for working with date and time values in Qt applications. It provides a wide range of functionality for parsing, formatting, comparison, and arithmetic operations.