(5)Qt中的日期和时间

QDate

日期对象格式化
    d              -    没有前导零的日子 (1 to 31)  
    dd            -    前导为0的日子   (01 to 31)  
    ddd          -    显示(缩写) 周一、周二、周三、周四、周五、周六、周日        
    dddd        -    显示(完整) 星期一、星期二、星期三、星期四、星期五、星期六、星期日
    
    M             -    没有前导零的月份(1到12)  
    MM          -    前导零的月份(01到12)  
    MMM       -    缩写 1月、2月、3月...         
    MMMM    -    完整 一月、二月、三月...
    
    yy            -    两个数字的年 (00 to 99)
    yyyy        -    以四位数表示的年份 

// 构造函数
QDate::QDate();
QDate::QDate(int y, int m, int d);

// 公共成员函数
// 重新设置日期对象中的日期
bool QDate::setDate(int year, int month, int day);
// 给日期对象添加 ndays 天
QDate QDate::addDays(qint64 ndays) const;
// 给日期对象添加 nmonths 月
QDate QDate::addMonths(int nmonths) const;
// 给日期对象添加 nyears 月
QDate QDate::addYears(int nyears) const;

// 得到日期对象中的年/月/日
int QDate::year() const;
int QDate::month() const;
int QDate::day() const;
void QDate::getDate(int *year, int *month, int *day) const;
 
QString QDate::toString(const QString &format) const;

// 操作符重载 ==> 日期比较
bool QDate::operator!=(const QDate &d) const;
bool QDate::operator<(const QDate &d) const;
bool QDate::operator<=(const QDate &d) const;
bool QDate::operator==(const QDate &d) const;
bool QDate::operator>(const QDate &d) const;
bool QDate::operator>=(const QDate &d) const;

// 静态函数 -> 得到本地的当前日期
[static] QDate QDate::currentDate();
//举个栗子
QDate date(2022, 10, 21);
qInfo() << date;
//获取年月日
qInfo() << date.year() << date.month() << date.day();
//设置年月日
date.setDate(1900, 10, 1);
qInfo() << date;
//添加时间
date = date.addDays(10);
qInfo() << date;

//将日期格式化成字符串
qInfo() << date.toString();
qInfo() << date.toString(Qt::DateFormat::ISODate);
qInfo() << date.toString("yyyy/MM/dd");

//获取当前系统日期
auto curDate = QDate::currentDate();
qInfo() << curDate;

QTime

时间格式化
    -- 时
    h       ==>    The hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
    hh     ==>    The hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
    H      ==>    The hour without a leading zero (0 to 23, even with AM/PM display)
    HH    ==>    The hour with a leading zero (00 to 23, even with AM/PM display)


    -- 分
    m       ==>    The minute without a leading zero (0 to 59)
    mm    ==>    The minute with a leading zero (00 to 59)


    -- 秒
    s      ==>    The whole second, without any leading zero (0 to 59)
    ss    ==>    The whole second, with a leading zero where applicable (00 to 59)


    -- 毫秒
    zzz    ==>    The fractional part of the second, to millisecond precision, 
            including trailing zeroes where applicable (000 to 999).


    -- 上午或者下午
    AP or A        ==>        使用AM/PM(大写) 描述上下午, 中文系统显示汉字
    ap or a         ==>        使用am/pm(小写) 描述上下午, 中文系统显示汉字 

// 构造函数
QTime::QTime();
/*
    h 			==> must be in the range 0 to 23
    m and s 	==> must be in the range 0 to 59
    ms 			==> must be in the range 0 to 999
*/ 
QTime::QTime(int h, int m, int s = 0, int ms = 0);

// 公共成员函数
// Returns true if the set time is valid; otherwise returns false.
bool QTime::setHMS(int h, int m, int s, int ms = 0);
QTime QTime::addSecs(int s) const;
QTime QTime::addMSecs(int ms) const;

// 示例代码
  QTime n(14, 0, 0);                // n == 14:00:00
  QTime t;
  t = n.addSecs(70);                // t == 14:01:10
  t = n.addSecs(-70);               // t == 13:58:50
  t = n.addSecs(10 * 60 * 60 + 5);  // t == 00:00:05
  t = n.addSecs(-15 * 60 * 60);     // t == 23:00:00

// 从时间对象中取出 时/分/秒/毫秒
// Returns the hour part (0 to 23) of the time. Returns -1 if the time is invalid.
int QTime::hour() const;
// Returns the minute part (0 to 59) of the time. Returns -1 if the time is invalid.
int QTime::minute() const;
// Returns the second part (0 to 59) of the time. Returns -1 if the time is invalid.
int QTime::second() const;
// Returns the millisecond part (0 to 999) of the time. Returns -1 if the time is invalid.
int QTime::msec() const;

QString QTime::toString(const QString &format) const;

// 操作符重载 ==> 时间比较
bool QTime::operator!=(const QTime &t) const;
bool QTime::operator<(const QTime &t) const;
bool QTime::operator<=(const QTime &t) const;
bool QTime::operator==(const QTime &t) const;
bool QTime::operator>(const QTime &t) const;
bool QTime::operator>=(const QTime &t) const;

// 静态函数 -> 得到当前时间
[static] QTime QTime::currentTime();
//举个栗子
QTime time(5, 2, 0);
qInfo() << time;
//获取属性
qInfo() << time.hour() << time.minute() << time.second();
//添加时间
time = time.addSecs(20);
qInfo() << time;
//将时间格式化成字符串
qInfo() << time.toString();
qInfo() << time.toString(Qt::DateFormat::ISODate);
qInfo() << time.toString("hh:mm:ss");
//从指定字符串生成时间
qInfo() << QTime::fromString("05:30:20");
//获取当前时间
QTime curTime = time.currentTime();
qInfo() << curTime;

QDateTime

// 构造函数
QDateTime::QDateTime();
QDateTime::QDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec = Qt::LocalTime);

// 公共成员函数
// 设置日期
void QDateTime::setDate(const QDate &date);
// 设置时间
void QDateTime::setTime(const QTime &time);
// 给当前日期对象追加 年/月/日/秒/毫秒, 参数可以是负数
QDateTime QDateTime::addYears(int nyears) const;
QDateTime QDateTime::addMonths(int nmonths) const;
QDateTime QDateTime::addDays(qint64 ndays) const;
QDateTime QDateTime::addSecs(qint64 s) const;
QDateTime QDateTime::addMSecs(qint64 msecs) const;

// 得到对象中的日期
QDate QDateTime::date() const;
// 得到对象中的时间
QTime QDateTime::time() const;

// 日期和时间格式, 格式字符参考QDate 和 QTime 类的 toString() 函数
QString QDateTime::toString(const QString &format) const;

// 操作符重载 ==> 日期时间对象的比较
bool QDateTime::operator!=(const QDateTime &other) const;
bool QDateTime::operator<(const QDateTime &other) const;
bool QDateTime::operator<=(const QDateTime &other) const;
bool QDateTime::operator==(const QDateTime &other) const;
bool QDateTime::operator>(const QDateTime &other) const;
bool QDateTime::operator>=(const QDateTime &other) const;

// 静态函数
// 得到当前时区的日期和时间(本地设置的时区对应的日期和时间)
[static] QDateTime QDateTime::currentDateTime();
//举个栗子
QDateTime dt = QDateTime::currentDateTime();
qInfo() << dt;
//获取日期
auto date = dt.date();
auto time = dt.time();
//格式化时间
qInfo() << dt.toString("yyyy年MM月dd日 hh:mm:ss AP");

经时计时器 

 QTime的经时计时器已经过时了,推荐使用QElapsedTimer。

//QTime已废弃的函数
// 开始计时
void QTime::start();
// 计时结束
int QTime::elapsed() const;
// 重新计时
int QTime::restart();

// 推荐使用的API函数
// QElapsedTimer 类
void QElapsedTimer::start();
qint64 QElapsedTimer::restart();
qint64 QElapsedTimer::elapsed() const;
//举个栗子
//经时计时器
QElapsedTimer eTimer;
eTimer.start();
int count = 0;
for (int i = 0; i < 100000000; i++)
{
    count++;
    //eTimer.restart();  //重新计时
}
qInfo() << "count:" << count << "用时:" << eTimer.elapsed(); //ms

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

石小浪♪

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

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

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

打赏作者

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

抵扣说明:

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

余额充值