QDateTime

1.两个时间戳做减法(差值)   

 QDateTime time1 = QDateTime::fromString("2019-09-19 18:40:02", "yyyy-MM-dd hh:mm:ss");
    
 QDateTime time2 = QDateTime::fromString("2019-09-19 18:43:02", "yyyy-MM-dd hh:mm:ss");
 
   
 qDebug() << "time2 - time1=" << time1.secsTo(time2);

输出为 180,单位为秒

 

  QDateTime d1=QDateTime::fromString("2021-01-10 18:00:00 000", "yyyy-MM-dd hh:mm:ss zzz");
  QDateTime d2=QDateTime::fromString("2021-01-11 18:00:00 000", "yyyy-MM-dd hh:mm:ss zzz");
  int timeSecond=d2.toTime_t()-d1.toTime_t();
  qDebug()<<"Second:"<<timeSecond;

Second: 86400

 

 

2. 时间格式  

  QString currentTime = QDateTime::currentDateTime ().toString ("yyyy_MM_dd hh:mm:ss zzz");
  qDebug()<<currentTime;

"2021_07_21 08:50:28 625"

 

3.时间加多少毫秒

 QDateTime dateTime=QDateTime::fromString("2021-01-11 18:00:00 000", "yyyy-MM-dd hh:mm:ss zzz");

  dateTime=dateTime.addMSecs(100);

  qDebug()<<dateTime;

QDateTime(2021-01-11 18:00:00.100 中国标准时间 Qt::LocalTime)

 

posted @ 2020-01-21 15:59  ike_li  阅读(916)  评论(0编辑  收藏  举报