• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Qt的日期和时间

武飞扬头像
小梁今天敲代码了吗
帮助1

目录

QDate

示例(打印年月日):

 QTime

示例(显示时分秒):

QDateTime

示例(显示当前日期和时间):

示例(分别取出 年 月 日 时 分 秒):

QDate

        

        QDate是Qt库中的日期类,提供了一种方便的方式来处理日期。它主要用于处理日期和时间相关的操作,包括日期的计算、格式化、比较和转换等。QDate可以处理的日期范围是从公元前4713年1月1日至公元7999年12月31日。

QDate的使用场景包括:  

1. 计算日期:QDate可以用于计算两个日期之间的天数、月数、年数,以及判断某一天是星期几等。 

2. 格式化日期:QDate可以将日期转换为不同的字符串格式,如"yyyy-MM-dd"、"dd.MM.yyyy"、"ddd MMM d yyyy"等。 

3. 比较日期:QDate提供了比较操作符,可以方便地比较两个日期的大小。 

4. 日期转换:QDate可以将日期转换为Unix时间戳或Julian日期等不同的日期格式。 

5. 绘制日期:QDate可以用于绘制日历等日期相关的界面。 

        总之,QDate是一个非常实用的日期处理类,在Qt开发中经常被使用。

  1.  
    // 构造函数
  2.  
    QDate::QDate();
  3.  
    QDate::QDate(int y, int m, int d);
  4.  
     
  5.  
    // 公共成员函数
  6.  
    // 重新设置日期对象中的日期
  7.  
    bool QDate::setDate(int year, int month, int day);
  8.  
    // 给日期对象添加 ndays 天
  9.  
    QDate QDate::addDays(qint64 ndays) const;
  10.  
    // 给日期对象添加 nmonths 月
  11.  
    QDate QDate::addMonths(int nmonths) const;
  12.  
    // 给日期对象添加 nyears 月
  13.  
    QDate QDate::addYears(int nyears) const;
  14.  
     
  15.  
    // 得到日期对象中的年/月/日
  16.  
    int QDate::year() const;
  17.  
    int QDate::month() const;
  18.  
    int QDate::day() const;
  19.  
    void QDate::getDate(int *year, int *month, int *day) const;
  20.  
     
  21.  
    // 日期对象格式化
  22.  
    /*
  23.  
    d - The day as a number without a leading zero (1 to 31)
  24.  
    dd - The day as a number with a leading zero (01 to 31)
  25.  
    ddd - The abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses the system locale to localize the name, i.e. QLocale::system().
  26.  
    dddd - The long localized day name (e.g. 'Monday' to 'Sunday'). Uses the system locale to localize the name, i.e. QLocale::system().
  27.  
    M - The month as a number without a leading zero (1 to 12)
  28.  
    MM - The month as a number with a leading zero (01 to 12)
  29.  
    MMM - The abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses the system locale to localize the name, i.e. QLocale::system().
  30.  
    MMMM - The long localized month name (e.g. 'January' to 'December'). Uses the system locale to localize the name, i.e. QLocale::system().
  31.  
    yy - The year as a two digit number (00 to 99)
  32.  
    yyyy - The year as a four digit number. If the year is negative, a minus sign is prepended, making five characters.
  33.  
    */
  34.  
    QString QDate::toString(const QString &format) const;
  35.  
     
  36.  
    // 操作符重载 ==> 日期比较
  37.  
    bool QDate::operator!=(const QDate &d) const;
  38.  
    bool QDate::operator<(const QDate &d) const;
  39.  
    bool QDate::operator<=(const QDate &d) const;
  40.  
    bool QDate::operator==(const QDate &d) const;
  41.  
    bool QDate::operator>(const QDate &d) const;
  42.  
    bool QDate::operator>=(const QDate &d) const;
  43.  
     
  44.  
    // 静态函数 -> 得到本地的当前日期
  45.  
    [static] QDate QDate::currentDate();
学新通
示例(打印年月日):

学新通

  1.  
    #include "mainwindow.h"
  2.  
    #include "ui_mainwindow.h"
  3.  
    #include <QDate>
  4.  
    #include <QTime>
  5.  
    MainWindow::MainWindow(QWidget *parent)
  6.  
    : QMainWindow(parent)
  7.  
    , ui(new Ui::MainWindow)
  8.  
    {
  9.  
    ui->setupUi(this);
  10.  
     
  11.  
    //获取当前日期
  12.  
    QDate d =QDate::currentDate();
  13.  
    //第一种方式
  14.  
    qDebug()<<"year:" <<d.year()<<", month: "<<d.month()<<",day: "<< d.day();
  15.  
    //第二种方式 2000-01-01
  16.  
    QString str = d.toString("yyyy-MM-dd");
  17.  
    qDebug()<<"date str: "<<str;
  18.  
     
  19.  
     
  20.  
     
  21.  
    }
  22.  
     
  23.  
    MainWindow::~MainWindow()
  24.  
    {
  25.  
    delete ui;
  26.  
    }
学新通

 运行结果:

学新通

 QTime

  QTime是Qt库中的时间类,提供了一种方便的方式来处理时间。它主要用于处理时间相关的操作,包括时间的计算、格式化、比较和转换等。QTime可以处理的时间范围是从0:0:0.000至23:59:59.999。

QTime的使用场景包括:

1. 计算时间:QTime可以用于计算两个时间之间的差值,包括小时数、分钟数、秒数和毫秒数。

2. 格式化时间:QTime可以将时间转换为不同的字符串格式,如"H:mm:ss.zzz"、"hh:mm AP"等。

3. 比较时间:QTime提供了比较操作符,可以方便地比较两个时间的大小。

4. 时间转换:QTime可以将时间转换为Unix时间戳或以毫秒为单位的时间等不同的时间格式。

5. 绘制时间:QTime可以用于绘制钟表等时间相关的界面。

  总之,QTime是一个非常实用的时间处理类,在Qt开发中经常被使用。

  1.  
    // 构造函数
  2.  
    QTime::QTime();
  3.  
    /*
  4.  
    h ==> 取值范围: 0 ~ 23
  5.  
    m and s ==> 取值范围: 0 ~ 59
  6.  
    ms ==> 取值范围: 0 ~ 999
  7.  
    */
  8.  
    QTime::QTime(int h, int m, int s = 0, int ms = 0);
  9.  
     
  10.  
    // 公共成员函数
  11.  
    // Returns true if the set time is valid; otherwise returns false.
  12.  
    bool QTime::setHMS(int h, int m, int s, int ms = 0);
  13.  
    QTime QTime::addSecs(int s) const;
  14.  
    QTime QTime::addMSecs(int ms) const;
  15.  
     
  16.  
    // 示例代码
  17.  
    QTime n(14, 0, 0); // n == 14:00:00
  18.  
    QTime t;
  19.  
    t = n.addSecs(70); // t == 14:01:10
  20.  
    t = n.addSecs(-70); // t == 13:58:50
  21.  
    t = n.addSecs(10 * 60 * 60 5); // t == 00:00:05
  22.  
    t = n.addSecs(-15 * 60 * 60); // t == 23:00:00
  23.  
     
  24.  
    // 从时间对象中取出 时/分/秒/毫秒
  25.  
    // Returns the hour part (0 to 23) of the time. Returns -1 if the time is invalid.
  26.  
    int QTime::hour() const;
  27.  
    // Returns the minute part (0 to 59) of the time. Returns -1 if the time is invalid.
  28.  
    int QTime::minute() const;
  29.  
    // Returns the second part (0 to 59) of the time. Returns -1 if the time is invalid.
  30.  
    int QTime::second() const;
  31.  
    // Returns the millisecond part (0 to 999) of the time. Returns -1 if the time is invalid.
  32.  
    int QTime::msec() const;
  33.  
     
  34.  
     
  35.  
    // 时间格式化
  36.  
    /*
  37.  
    -- 时 --
  38.  
    h ==> The hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
  39.  
    hh ==> The hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
  40.  
    H ==> The hour without a leading zero (0 to 23, even with AM/PM display)
  41.  
    HH ==> The hour with a leading zero (00 to 23, even with AM/PM display)
  42.  
    -- 分 --
  43.  
    m ==> The minute without a leading zero (0 to 59)
  44.  
    mm ==> The minute with a leading zero (00 to 59)
  45.  
    -- 秒 --
  46.  
    s ==> The whole second, without any leading zero (0 to 59)
  47.  
    ss ==> The whole second, with a leading zero where applicable (00 to 59)
  48.  
    -- 毫秒 --
  49.  
    zzz ==> The fractional part of the second, to millisecond precision,
  50.  
    including trailing zeroes where applicable (000 to 999).
  51.  
    -- 上午或者下午
  52.  
    AP or A ==> 使用AM/PM(大写) 描述上下午, 中文系统显示汉字
  53.  
    ap or a ==> 使用am/pm(小写) 描述上下午, 中文系统显示汉字
  54.  
    */
  55.  
    QString QTime::toString(const QString &format) const;
  56.  
     
  57.  
    // 阶段性计时
  58.  
    // 过时的API函数
  59.  
    // 开始计时
  60.  
    void QTime::start();
  61.  
    // 计时结束
  62.  
    int QTime::elapsed() const;
  63.  
    // 重新计时
  64.  
    int QTime::restart();
  65.  
     
  66.  
    // 推荐使用的API函数
  67.  
    // QElapsedTimer 类
  68.  
    void QElapsedTimer::start();
  69.  
    qint64 QElapsedTimer::restart();
  70.  
    qint64 QElapsedTimer::elapsed() const;
  71.  
     
  72.  
     
  73.  
    // 操作符重载 ==> 时间比较
  74.  
    bool QTime::operator!=(const QTime &t) const;
  75.  
    bool QTime::operator<(const QTime &t) const;
  76.  
    bool QTime::operator<=(const QTime &t) const;
  77.  
    bool QTime::operator==(const QTime &t) const;
  78.  
    bool QTime::operator>(const QTime &t) const;
  79.  
    bool QTime::operator>=(const QTime &t) const;
  80.  
     
  81.  
    // 静态函数 -> 得到当前时间
  82.  
    [static] QTime QTime::currentTime();
学新通
示例(显示时分秒):

学新通

  1.  
    #include "mainwindow.h"
  2.  
    #include "ui_mainwindow.h"
  3.  
    #include <QDate>
  4.  
    #include <QTime>
  5.  
    MainWindow::MainWindow(QWidget *parent)
  6.  
    : QMainWindow(parent)
  7.  
    , ui(new Ui::MainWindow)
  8.  
    {
  9.  
    ui->setupUi(this);
  10.  
    //获取当前时间
  11.  
    QTime curtime = QTime::currentTime();
  12.  
    //方式1
  13.  
    qDebug()<<"hour: "<<curtime.hour() <<",minute: "<<curtime.minute()
  14.  
    <<", second: "<<curtime.second()<<" , millisecond: "<<curtime.msec();
  15.  
    //方式2
  16.  
    QString strm = curtime.toString("hh:mm:ss.zzz");
  17.  
    qDebug()<<"格式化的日期: "<<strm;
  18.  
    }
  19.  
     
  20.  
    MainWindow::~MainWindow()
  21.  
    {
  22.  
    delete ui;
  23.  
    }
学新通

 运行结果:

学新通

QDateTime

  QDateTime是Qt库中的日期时间类,它可以同时处理日期和时间信息。QDateTime提供了一系列的函数来处理日期和时间的格式化、比较、计算和转换等操作。

QDateTime的使用场景包括:

1. 使计算时间:QDateTime可以用于计算两个日期之间的差值、计算某个日期之前或之后的几天、几个月或几年等。

2. 格式化时间:QDateTime可以将日期时间转换为不同的字符串格式,如"yyyy-MM-dd", "hh:mm:ss zzz"等。

3. 比较时间:QDateTime提供了比较操作符,可以方便地比较两个日期时间的大小,判断哪一个更早或更晚。

4. 时间转换:QDateTime可以将日期时间转换为Unix时间戳或以毫秒为单位的时间等不同的时间格式。

5. 绘制时间:QDateTime可以用于绘制日历等日期时间相关的界面。

  总之,QDateTime是一个非常实用的日期时间处理类,在Qt开发中经常被使用。无论是处理时间戳,构建日历,或是格式日期时间,QDateTime都是一个非常实用的工具。

  1.  
    // 构造函数
  2.  
    QDateTime::QDateTime();
  3.  
    QDateTime::QDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec = Qt::LocalTime);
  4.  
     
  5.  
    // 公共成员函数
  6.  
    // 设置日期
  7.  
    void QDateTime::setDate(const QDate &date);
  8.  
    // 设置时间
  9.  
    void QDateTime::setTime(const QTime &time);
  10.  
    // 给当前日期对象追加 年/月/日/秒/毫秒, 参数可以是负数
  11.  
    QDateTime QDateTime::addYears(int nyears) const;
  12.  
    QDateTime QDateTime::addMonths(int nmonths) const;
  13.  
    QDateTime QDateTime::addDays(qint64 ndays) const;
  14.  
    QDateTime QDateTime::addSecs(qint64 s) const;
  15.  
    QDateTime QDateTime::addMSecs(qint64 msecs) const;
  16.  
     
  17.  
    // 得到对象中的日期
  18.  
    QDate QDateTime::date() const;
  19.  
    // 得到对象中的时间
  20.  
    QTime QDateTime::time() const;
  21.  
     
  22.  
    // 日期和时间格式, 格式字符参考QDate 和 QTime 类的 toString() 函数
  23.  
    QString QDateTime::toString(const QString &format) const;
  24.  
     
  25.  
     
  26.  
    // 操作符重载 ==> 日期时间对象的比较
  27.  
    bool QDateTime::operator!=(const QDateTime &other) const;
  28.  
    bool QDateTime::operator<(const QDateTime &other) const;
  29.  
    bool QDateTime::operator<=(const QDateTime &other) const;
  30.  
    bool QDateTime::operator==(const QDateTime &other) const;
  31.  
    bool QDateTime::operator>(const QDateTime &other) const;
  32.  
    bool QDateTime::operator>=(const QDateTime &other) const;
  33.  
     
  34.  
    // 静态函数
  35.  
    // 得到当前时区的日期和时间(本地设置的时区对应的日期和时间)
  36.  
    [static] QDateTime QDateTime::currentDateTime();
学新通
示例(显示当前日期和时间):

学新通

  1.  
    #include "mainwindow.h"
  2.  
    #include "ui_mainwindow.h"
  3.  
    #include <QDateTime>
  4.  
    MainWindow::MainWindow(QWidget *parent)
  5.  
    : QMainWindow(parent)
  6.  
    , ui(new Ui::MainWindow)
  7.  
    {
  8.  
    ui->setupUi(this);
  9.  
    //获取当前的日期和时间
  10.  
    QDateTime dt =QDateTime::currentDateTime();
  11.  
    //格式化 yyyy//MM/dd hh:mm:ss ap
  12.  
    QString strdt = dt.toString("yyyy//MM/dd hh:mm:ss ap");
  13.  
    QString strdt1 = dt.toString("yyyy//MM/dd hh:mm:ss ");
  14.  
    QString strdt2 = dt.toString("yyyy//MM/dd HH:mm:ss ap");
  15.  
    qDebug()<<"当前的日期和时间:"<<strdt;
  16.  
    qDebug()<<"当前的日期和时间:"<<strdt1;
  17.  
    qDebug()<<"当前的日期和时间:"<<strdt2;
  18.  
     
  19.  
     
  20.  
    }
  21.  
     
  22.  
    MainWindow::~MainWindow()
  23.  
    {
  24.  
    delete ui;
  25.  
    }
学新通

 运行结果:

学新通

 可以看到当我们采用的格式不同时,得到的日期时间显示结果也不同

示例(分别取出 年 月 日 时 分 秒):

学新通

  1.  
    #include "mainwindow.h"
  2.  
    #include "ui_mainwindow.h"
  3.  
    #include <QDate>
  4.  
    #include <QTime>
  5.  
    #include <QDateTime>
  6.  
    MainWindow::MainWindow(QWidget *parent)
  7.  
    : QMainWindow(parent)
  8.  
    , ui(new Ui::MainWindow)
  9.  
    {
  10.  
    ui->setupUi(this);
  11.  
     
  12.  
    QDateTime dt =QDateTime::currentDateTime();
  13.  
    //先取出日期
  14.  
    QDate d = dt.date();
  15.  
    qDebug()<<"year: "<<d.year()<<", month:"<<d.month()<<" , day:"<<d.day();
  16.  
    //再取出时间
  17.  
    QTime t = dt.time();
  18.  
    qDebug()<<"hour: "<<t.hour() <<",minute: "<<t.minute()
  19.  
    <<", second: "<<t.second()<<" , millisecond: "<<t.msec();
  20.  
     
  21.  
    }
  22.  
     
  23.  
    MainWindow::~MainWindow()
  24.  
    {
  25.  
    delete ui;
  26.  
    }
学新通

运行结果:

学新通

总结:本文讲解了Qt中会使用到的时间和日期的相关知识 

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhghcbbg
系列文章
更多 icon
同类精品
更多 icon
继续加载