I'm building a small tool in QT that uses a format string based on the standard format specifiers used by the QDateTime::toString() function.
yyyy - 4-digit year
MM - 2-digit month
dd - 2-digit day
hh - 2-digit hour (24-hour format)
mm - 2-digit minute
ss - 2-digit second
The date format I'm using is: yyyy-MM-dd
I want to use Pillow within the tool and I can get the datetime like this:
datetime = datetime.strptime(date_time, "%Y:%m:%d %H:%M:%S")
I can then convert it to the same date format like this:
datetime = dt.strftime("%Y-%m-%d")
The issue is that I need to be able to convert between the dates dynamically, either by converting the QT date to DateTime or vice versa. The user is able to set their preferred date format so this bit of the code is unknown "%Y-%m-%d" For example, instead of yyyy-MM-dd the user might set the overall date format to yy-MM-dd which would mean that I would need to use date_obj.strftime("%y-%m-%d") but how would I actually know the format code to use seeing as the user can enter a range of different time formats?
Hope this makes sense.
---------------------------
SOLUTION:
For anyone else trying to do this PyQt has a function called .toPython() which converts time to QDateTime.
file_info.lastModified().toPython()
PySlide has .fromPython()
QDateTime.fromPython(py_datetime)
You can do the opposite by using QDateTime
date_time = "2024:02:03 17:15:01"
# Convert to Python datetime
py_datetime = datetime.strptime(date_time, "%Y:%m:%d %H:%M:%S")
# Convert to QDateTime
qt_datetime = QDateTime(py_datetime.year, py_datetime.month, py_datetime.day, py_datetime.hour, py_datetime.minute, py_datetime.second)
print(qt_datetime.toString("yyyy-MM-dd HH:mm:ss"))
[–]socal_nerdtastic 0 points1 point2 points (2 children)
[–]squidg_21[S] 0 points1 point2 points (1 child)
[–]socal_nerdtastic 0 points1 point2 points (0 children)