qt-03-QDateTime

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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发如雪-ty

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值