1.获取当前时间
QDateTime m_curtTime = QDateTime::currentDateTime();
2.时间转QString
QString strcurTime = m_curtTime.toString("yyyy-MM-dd hh:mm:ss");
cout<<strcurTime.toUtf8().data()<<endl;
3.字符串转QDateTime
QString strTestTime = "2020-05-20 08:54:11";
QDateTime t1 = QDateTime::fromString(strTestTime,"yyyy-MM-dd hh:mm:ss");
QString strt2 = t1.toString("yyyy-MM-dd hh:mm:ss");
cout<<strt2.toUtf8().data()<<endl;
4.时间差
QDateTime t11(QDate(2020,8,10),QTime(9,4,11));
QDateTime t12(QDate(2020,8,11),QTime(9,34,55));
//time1到time2的秒数
qint64 second = t11.secsTo(t12);
cout<<"second:"<<second<<endl;
//time1到time2的天数
qint64 day = t11.daysTo(t12);
cout<<"days:"<<day<<endl;
QString strTmp = t11.toString("yyyy-MM-dd hh:mm:ss");
cout<<"strTmp:"<<strTmp.toUtf8().data()<<endl;
t11 = t11.addSecs(3600);//一小时
strTmp = t11.toString("yyyy-MM-dd hh:mm:ss");
cout<<"strTmp:"<<strTmp.toUtf8().data()<<endl;
5.时间戳
// 1.toTime_t()把2014年12月19日10:24:40这样的QDateTime的格式转变为1418955940这样的时间戳
QDateTime time = QDateTime::currentDateTime(); //获取当前时间
int timeT = time.toTime_t(); //将当前时间转为时间戳
cout<<"时间转时间戳:"<<timeT<<endl;
QDateTime timex = QDateTime::fromTime_t(timeT);
strTmp = timex.toString("yyyy-MM-dd hh:mm:ss");
cout<<"时间戳转时间:"<<strTmp.toUtf8().data()<<endl;