获取日期和时间使用QDateTime类,该类中有一个静态成员函数可以返回当前的时间信息
我们可以直接调用这个静态函数获取当前时间
QDateTime time = QDateTime::currentDateTime(); qInfo() << time;
结果是
QDateTime(2021-06-26 09:15:08.096 中国标准时间 Qt::LocalTime)
如果想要输出成类似2021-06-26 09:15:08的格式,就需要用到QDateTime中的toString方法
示例:
QDateTime time = QDateTime::currentDateTime(); qInfo() << time.toString("yyyy-MM-dd ddd hh:mm");
输出结果为:"2021-06-26 周六 09:21"