how to find date time diffrence between date time in format "yyyy-mm-dd hh:mm:ss" using QDateTime ?
-
Can any body help me to get current date time in format like yyyy-mm-dd hh:mm:ss for example :"2022-11-14 09:34:07".
i want to also find difference between current date time and older date time. so if anybody know how to find it then please let me know with example.
-
@Bonnie Hi want to find difference between date time
i.e. difference between 2022-11-14 09:34:07 and 2022-11-14 12:17:54
how many years, months, days, hours , seconds and millisecond get spent between this dates ?
QDateTime dt1 = QDateTime::fromString("2022-11-14 09:34:07", "yyyy-MM-dd hh:mm:ss"); QDateTime dt2 = QDateTime::fromString("2022-11-14 12:17:54", "yyyy-MM-dd hh:mm:ss"); qint64 secondsbetweenDateTime = dt1.secsTo(dt2);
-
QDateTime date = QDateTime::currentDateTime(); QString formattedTime = date.toString("yyyy-MM-dd hh:mm:ss");
-
- Edit: sorry wrong understanding, I see you've already figured it out.
- What kind of difference do you want? There is QDateTime::secsTo that returns the number of seconds from a datetime to another
-
- Edit: sorry wrong understanding, I see you've already figured it out.
- What kind of difference do you want? There is QDateTime::secsTo that returns the number of seconds from a datetime to another
@Bonnie Hi want to find difference between date time
i.e. difference between 2022-11-14 09:34:07 and 2022-11-14 12:17:54
how many years, months, days, hours , seconds and millisecond get spent between this dates ?
-
@Bonnie Hi want to find difference between date time
i.e. difference between 2022-11-14 09:34:07 and 2022-11-14 12:17:54
how many years, months, days, hours , seconds and millisecond get spent between this dates ?
@Qt-embedded-developer As I posted, there are
secsTo
in QDateTime, and alsomsecsTo
anddaysTo
.
Others you would need to calculate them by yourself depending on the results of the above functions. -
@Bonnie Hi want to find difference between date time
i.e. difference between 2022-11-14 09:34:07 and 2022-11-14 12:17:54
how many years, months, days, hours , seconds and millisecond get spent between this dates ?
QDateTime dt1 = QDateTime::fromString("2022-11-14 09:34:07", "yyyy-MM-dd hh:mm:ss"); QDateTime dt2 = QDateTime::fromString("2022-11-14 12:17:54", "yyyy-MM-dd hh:mm:ss"); qint64 secondsbetweenDateTime = dt1.secsTo(dt2);