c++qtqdatetime

Subtract 40 minutes from current time


I would like to use QDateTime to represent the value of 40 minutes less than the current time as a string.

QDateTime time = QDateTime::currentDateTime() - QDateTime::fromString(QDateTime::currentDateTime().toString("yy/MM/dd hh:40:00"), "yy/MM/dd hh:mm:ss");

I tried that. but, It's didn't working.

I want a string minus 40 minutes from the current time and date.


Solution

  • QDateTime has an addSecs method. You can add -40 * 60 seconds to effectively subtract 40 minutes.