qt DateTime 计算时间

获取当前时间

QDateTime t1 = QDateTime::currentDateTime();
qDebug() << t1.toString("yyyy-MM-dd hh:mm:ss");

string to QDateTime

QDateTime t1 = QDateTime::fromString("2020-08-03 00:00:00", "yyyy-MM-dd hh:mm:ss");
qDebug() << t1.toString("yyyy-MM-dd hh:mm:ss");

计算时间差

QDateTime t1 = QDateTime::fromString("2020-08-03 00:00:00", "yyyy-MM-dd hh:mm:ss");
qDebug() << t1.toString("yyyy-MM-dd hh:mm:ss");

QDateTime t2 = QDateTime::fromString("2020-08-03 00:00:10", "yyyy-MM-dd hh:mm:ss");
qDebug() << t2.toString("yyyy-MM-dd hh:mm:ss");

int seconds = t2.toTime_t() - t1.toTime_t();
qDebug() << "相差:" << seconds << "秒"; // 相差: 10 秒
qDebug() << "相差:" << seconds * 1000 << "毫秒"; // 相差: 10000 毫秒

int msecs = t2.toMSecsSinceEpoch() - t1.toMSecsSinceEpoch(); // 相差: 10000 毫秒
qDebug() << "相差:" << msecs << "毫秒";
posted @ 2020-09-03 16:13  Ajanuw  阅读(1509)  评论(0编辑  收藏  举报