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 << "毫秒";


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM