QDateTime

目录

1、构造

2、获取时间

3、增加时间

4、设置时间

5、时间差

6、判断时间是否有效

7、测试代码


1、构造

QDateTime time;
QDateTime time1(QDate::currentDate());
QDateTime time2(QDate::currentDate(),QTime::currentTime());
QDateTime time3(time1);
//QDateTime time4(&other); //移动构造
qDebug() << time1;
qDebug() << time2;
qDebug() << time3;

2、获取时间

qDebug() << time1.date();     //返回日期
qDebug() << time1.time();     //返回时分秒
qDebug() << time1.timeSpec(); //时区
qDebug() << time1.timeZoneAbbreviation(); //返回日期时间的时区缩写
qDebug() << time1.toLocalTime();          //返回时间
qDebug() << time1.toMSecsSinceEpoch();    //返回自1970.0.0 0:0:0 到现在的UTC毫秒数
qDebug() << time1.toOffsetFromUtc(100);   //返回偏移后的时间,参数单位分钟
qDebug() << time1.toSecsSinceEpoch();     //返回自1970.0.0 0:0:0 到现在的UTC秒数
qDebug() << time1.toUTC();                //返回UTC时间
//将时间转为字符串
qDebug() << time1.toString();
qDebug() << time1.toString("dd.MM.yyyy");
qDebug() << time1.toString("ddd MMMM d yy");
qDebug() << time1.toString("hh:mm:ss.zzz");
qDebug() << time1.toString("hh:mm:ss.z");
qDebug() << time1.toString("h:m:s ap");
//将字符串转为时间
time1 = QDateTime::fromString(time1.toString());
time1 = QDateTime::fromString("07.12.2019","dd.MM.yyyy");
qDebug() << QDateTime::currentDateTimeUtc();    //返回UTC时间
qDebug() << QDateTime::currentDateTime();       //返回当前系统时间
qDebug() << QDateTime::currentDateTimeUtc();    //返回当前系统时间相对的UTC时间,比北京时间慢8小时
qDebug() << QDateTime::currentMSecsSinceEpoch();//返回当前系统时间的毫秒单位时间
qDebug() << QDateTime::currentSecsSinceEpoch(); //返回当前系统时间的秒单位时间

3、增加时间

qDebug() << time1.addYears(10); //年
qDebug() << time1.addMonths(10);//月
qDebug() << time1.addDays(10);  //日
qDebug() << time1.addSecs(10);  //秒
qDebug() << time1.addMSecs(10); //毫秒

4、设置时间

qDebug() <<"设置时间前"<< time1;
QDate date = QDate::currentDate();
date = date.addYears(100);
QTime qTime = QTime::currentTime();
qTime = qTime.addSecs(100);

time1.setDate(date);    //设置日期
time1.setTime(qTime);   //设置时间
qDebug() << time1;

5、时间差

qDebug() << time2.secsTo(time1);    //秒
qDebug() << time2.daysTo(time1);    //天
qDebug() << time2.msecsTo(time1);   //毫秒

6、判断时间是否有效

qDebug() << time2.isNull();     //是否是空值
qDebug() << time2.isValid();    //时间是否有效

7、测试代码

#include <QCoreApplication>
#include <QDebug>
#include <QDateTime>

using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //构造
    QDateTime time;
    QDateTime time1(QDate::currentDate());
    QDateTime time2(QDate::currentDate(),QTime::currentTime());
    QDateTime time3(time1);
    //QDateTime time4(&other); //移动构造
    qDebug() << time1;
    qDebug() << time2;
    qDebug() << time3;

    //获取时间
    qDebug() << time1.date();     //返回日期
    qDebug() << time1.time();     //返回时分秒
    qDebug() << time1.timeSpec(); //时区
    qDebug() << time1.timeZoneAbbreviation(); //返回日期时间的时区缩写
    qDebug() << time1.toLocalTime();          //返回时间
    qDebug() << time1.toMSecsSinceEpoch();    //返回自1970.0.0 0:0:0 到现在的UTC毫秒数
    qDebug() << time1.toOffsetFromUtc(100);   //返回偏移后的时间,参数单位分钟
    qDebug() << time1.toSecsSinceEpoch();     //返回自1970.0.0 0:0:0 到现在的UTC秒数
    qDebug() << time1.toUTC();                //返回UTC时间
    //将时间转为字符串
    qDebug() << time1.toString();
    qDebug() << time1.toString("dd.MM.yyyy");
    qDebug() << time1.toString("ddd MMMM d yy");
    qDebug() << time1.toString("hh:mm:ss.zzz");
    qDebug() << time1.toString("hh:mm:ss.z");
    qDebug() << time1.toString("h:m:s ap");
    //将字符串转为时间
    time1 = QDateTime::fromString(time1.toString());
    time1 = QDateTime::fromString("07.12.2019","dd.MM.yyyy");
    qDebug() << QDateTime::currentDateTimeUtc();    //返回UTC时间
    qDebug() << QDateTime::currentDateTime();       //返回当前系统时间
    qDebug() << QDateTime::currentDateTimeUtc();    //返回当前系统时间相对的UTC时间,比北京时间慢8小时
    qDebug() << QDateTime::currentMSecsSinceEpoch();//返回当前系统时间的毫秒单位时间
    qDebug() << QDateTime::currentSecsSinceEpoch(); //返回当前系统时间的秒单位时间


    //增加时间
    qDebug() << time1.addYears(10); //年
    qDebug() << time1.addMonths(10);//月
    qDebug() << time1.addDays(10);  //日
    qDebug() << time1.addSecs(10);  //秒
    qDebug() << time1.addMSecs(10); //毫秒

    //设置时间
    qDebug() <<"设置时间前"<< time1;
    QDate date = QDate::currentDate();
    date = date.addYears(100);
    QTime qTime = QTime::currentTime();
    qTime = qTime.addSecs(100);

    time1.setDate(date);    //设置日期
    time1.setTime(qTime);   //设置时间
    qDebug() << time1;

    //时间差
    qDebug() << time2.secsTo(time1);    //秒
    qDebug() << time2.daysTo(time1);    //天
    qDebug() << time2.msecsTo(time1);   //毫秒
    //qDebug() << Qt::TimeSpec::LocalTime;

    //判断时间是否有效
    qDebug() << time2.isNull();     //是否是空值
    qDebug() << time2.isValid();    //时间是否有效

    return a.exec();
}
发布了44 篇原创文章 · 获赞 54 · 访问量 7051

猜你喜欢

转载自blog.csdn.net/Liu_Xiao_Ming/article/details/103434266
今日推荐