QDateTime类
QDateTime
对象包含日历日期和时钟时间。它是QDate
和QTime
类的组合。它有许多类似的方法,用法与这两个类相同。
// datetime.cpp
#include <QTextStream>
#include <QDateTime>
int main(void) {
QTextStream out(stdout);
// 获取当前时间
QDateTime cdt = QDateTime::currentDateTime();
out << "The current datetime is " << cdt.toString() << endl;
// 通过date()方法获取日期
out << "The current date is " << cdt.date().toString() << endl;
// 通过time()方法获取时间
out << "The current time is " << cdt.time().toString() << endl;
}
输出结果为:
$ ./datetime
The current datetime is 周二 2月 20 11:05:19 2018
The current date is 周二 2月 20 2018
The current time is 11:05:19