您的位置:首页 > 编程语言 > Qt开发

[Qt] Qt时间 - QDateTime

2015-07-08 10:23 681 查看
QDateTime dateTime;

QString dateTime_str = dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss");

// 从字符串转换为毫秒(需完整的年月日时分秒)

datetime.fromString("2011-09-10 12:07:50:541", "yyyy-MM-dd hh:mm:ss:zzz").toMSecsSinceEpoch();

// 从字符串转换为秒(需完整的年月日时分秒)

datetime.fromString("2011-09-10 12:07:50:541", "yyyy-MM-dd hh:mm:ss:zzz").toTime_t();

// 从毫秒转换到年月日时分秒

datetime.fromMSecsSinceEpoch(1315193829218).toString("yyyy-MM-dd hh:mm:ss:zzz");

// 从秒转换到年月日时分秒(若有zzz,则为000)

datetime.fromTime_t(1315193829).toString("yyyy-MM-dd hh:mm:ss[:zzz]");

- 获取系统时间

#include <QDateTime>
#include <QDebug>

...

QDateTime sysDateTime;

qDebug() <<sysDateTime.currentDateTime().toString("yyyy年MM月dd日 hh:mm:ss");

- 延时(4.7之前的版本不能使用)

#include <QApplication>
#include <QDateTime>
#include <QDebug>
...

qint64 startTime = QDateTime::currentMSecsSinceEpoch();

qDebug() << startTime;

while (1)

{

if (QDateTime::currentMSecsSinceEpoch() - startTime > interval) // interval为需要延时的时间(ms)

{

break;

}

QApplication::processEvents(); // 处理其他事件,避免程序出现假死

}

qDebug() << QDateTime::currentMSecsSinceEpoch();

- 计算2个操作的时间差

#include <QTime>
#include <QDebug>
...
QTime startTime = QTime::currentTime();

QTime endTime = QTime::currentTime();

qDebug() << startTime.msecsTo(endTime); // 结果为ms

版权声明:本文为博主原创文章,未经博主允许不得转载。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: