QT中日期和时间类

news2024/9/28 1:19:39

QT中日期和时间类

  • QDate
  • QTime
  • QDateTime

QDate

QDate类可以封装日期信息也可以通过这个类得到日期相关的信息, 包括:年, 月, 日。

// 构造函数
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;

// 日期对象格式化
/*
    d    - The day as a number without a leading zero (1 to 31)
    dd   - The day as a number with a leading zero (01 to 31)
    ddd	 - The abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses the system locale to localize the name, i.e. QLocale::system().
    dddd - The long localized day name (e.g. 'Monday' to 'Sunday'). Uses the system locale to localize the name, i.e. QLocale::system().
    M    - The month as a number without a leading zero (1 to 12)
    MM   - The month as a number with a leading zero (01 to 12)
    MMM	 - The abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses the system locale to localize the name, i.e. QLocale::system().
    MMMM - The long localized month name (e.g. 'January' to 'December'). Uses the system locale to localize the name, i.e. QLocale::system().
    yy   - The year as a two digit number (00 to 99)
    yyyy - The year as a four digit number. If the year is negative, a minus sign is prepended, making five characters.
*/
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;

// 静态函数 -> 得到本地的当前日期

例子:

    QDate d2(2023,7,28);

    qDebug()<<d2;
    d2 = d2.addYears(1);
    d2 = d2.addMonths(1);
    d2 = d2.addDays(1);
    qDebug()<<d2;
    d2.setDate(2011,5,22);
    qDebug()<<d2;
    QString s2 = d2.toString();
    qDebug()<<s2;
    qDebug()<<"----------";
    QDate d1(2011,2,22);
    qDebug()<<(d1<d2);

在这里插入图片描述

格式化输出日期:

    QDate d3 = QDate::currentDate();
    qDebug()<<d3;
    qDebug()<<"year "<<d3.year()<<"month "<<d3.month()<<"day"<<d3.day();
    QString s31 = "M";
    s31 = d3.toString("yyyy-MM-dd");
    qDebug()<<s31;
    QString s32 = d3.toString("yyy-MMMM-dddd");
    qDebug()<<s32;

在这里插入图片描述

QTime

QTime类可以封装时间信息也可以通过这个类得到时间相关的信息, 包括:时, 分, 秒, 毫秒

// 构造函数
QTime::QTime();
/*
    h 		==> 取值范围: 0 ~ 23
    m and s 	==> 取值范围: 0 ~ 59
    ms 		==> 取值范围: 0 ~ 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;


// 时间格式化
/*
    -- 时 --
    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)

例子:

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

在这里插入图片描述

    int h = n.hour();
    int m = n.minute();
    int s = n.second();
    int ms = n.msec();
    qDebug()<<"h :"<<h<<" m: "<<m<<" s: "<<s<<"ms: "<<ms;

在这里插入图片描述

格式化输出;

    QString s4 = n.toString("h---m---s----");
    qDebug()<<s4;
    QString s5 = n.toString("h---m---s----zz");
    qDebug()<<s5;

在这里插入图片描述

    //获取当前时间
    QTime t5 = QTime::currentTime();
    //显示时间 (方式1)
    qDebug()<<"h :"<<t5.hour()<<" m: "<<t5.minute()<<" s: "<<t5.second()<<"ms: "<<t5.msec();
    //方式2
    QString s52 =t5.toString("hh-mm-ss.zz");
    qDebug()<<s52;

在这里插入图片描述

QDateTime

QDateTime类可以封装日期和时间信息也可以通过这个类得到日期和时间相关的信息, 包括:年, 月, 日, 时, 分, 秒, 毫秒。其实这个类就是QDate 和 QTime 这两个类的结合体。

// 构造函数
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 dt1 = QDateTime::currentDateTime();
    qDebug()<<dt1;
    QString s1 = dt1.toString("yyyy/MM/dd hh:mm:ss ap");
    qDebug()<<s1;

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/805078.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【数据结构与算法】整合一

GitHub同步更新&#xff08;已分类&#xff09;&#xff1a;Data_Structure_And_Algorithm-Review 公众号&#xff1a;URLeisure 的复习仓库 公众号二维码见文末 以下是本篇文章正文内容&#xff0c;下面案例可供参考。 吐血整理数据结构合集一&#xff1a; 整理了之前发的文…

SpringBoot集成Thymeleaf

Spring Boot 集成 Thymeleaf 模板引擎 1、Thymeleaf 介绍 Thymeleaf 是适用于 Web 和独立环境的现代服务器端 Java 模板引擎。 Thymeleaf 的主要目标是为开发工作流程带来优雅的自然模板&#xff0c;既可以在浏览器中正确显示的 HTML&#xff0c;也可以用作静态原型&#xf…

华为云NFS使用API删除大文件目录

最近在使用华为云SFS时&#xff0c;如果一个目录存储文件数超过100W&#xff0c;执行 “rm -rf path”时&#xff0c;存在删不动的情况&#xff0c;可以使用华为云API接口&#xff0c;执行异步删除。 华为官网&#xff1a; 删除文件系统目录_弹性文件服务 SFS_API参考_SFS Tu…

八方锦程15周年巡讲重磅启动,上海首场巡讲圆满举行

7月27日,八方锦程15周年巡讲——“与行业大咖同行 打造卓越人力资源官”活动在上海圆满举行! 作为八方锦程15周年巡讲的第一站,这一时刻,近200位与会人力资源从业者共同见证。 活动由八方锦程人才研究院副院长Leon主持并致开幕词。他向与会人员分享了举办该系列活动的初心,以八…

Mybatis源码-三种SqlSession的区别

三个SqlSession DefaultSqlSession与SqlSessionManager 与SqlSessionTemplate 是我常见的3种sqlsesion 从类图可以看出他们三个都实现了了SqlSession&#xff0c;也就是他们都可以表示一个会话。与其他不同的是SqlSessionManager实现了SqlSessionFactory 这三种sqlsession的区…

Windows 找不到文件‘chrome‘。请确定文件名是否正确后,再试一次

爱像时间&#xff0c;永恒不变而又短暂&#xff1b;爱像流水&#xff0c;浩瀚壮阔却又普普通通。 Windows 找不到文件chrome。请确定文件名是否正确后&#xff0c;再试一次 如果 Windows 提示找不到文件 "chrome"&#xff0c;可能是由于以下几种原因导致的&#xff1…

vmware中windows操作系统虚拟机安装

1.win10中安装 1.1 虚拟机向导 文件-新建虚拟机 典型-下一步 稍后安装操作系统-下一步 window10 64x -下一步 修改虚拟机名称及位置-下一步 默认60g,至少大于40g-将虚拟磁盘拆分成多个文件夹-下一步 点击完成 1.2 编辑虚拟机设置 移除打印机 设置虚拟机&#xff0c;加入iso映…

DAY4,Qt(事件处理机制的使用,Qt中实现服务器的原理)

1.Qt中实现服务器的原理&#xff1b; ---chatser.h---头文件 #ifndef CHATSER_H #define CHATSER_H#include <QWidget> #include <QTcpServer> //服务器类 #include <QTcpSocket> //套接字类 #include <QMessageBox> //消息对话类 #include <Q…

机器学习深度学习——模型选择、欠拟合和过拟合

&#x1f468;‍&#x1f393;作者简介&#xff1a;一位即将上大四&#xff0c;正专攻机器学习的保研er &#x1f30c;上期文章&#xff1a;机器学习&&深度学习——多层感知机的简洁实现 &#x1f4da;订阅专栏&#xff1a;机器学习&&深度学习 希望文章对你们有…

HHDESK便捷功能介绍三

1 连接便捷显示 工作中&#xff0c;往往需要设置很多资源连接。而过多的连接设&#xff0c;往往很容易混淆。 在HHDESK中&#xff0c;当鼠标点击连接时&#xff0c;会在下方显示本连接的参数&#xff0c;方便用户查看。 2 日志查看 实际工作中&#xff0c;查看日志是一件很…

干货 | 5个经典的模拟电路解析,电子人必看!

干货 | 5个经典的模拟电路解析&#xff0c;电子人必看&#xff01; 作为一个电子人&#xff0c;我们平时需要和不同的电路接触&#xff0c;但有一些电路图是经典的&#xff0c;值得我们永远记住。一、自举电路 此电路用在各种ADC之前的采样电路&#xff0c;可以让ADC实现轨到轨…

lc154.寻找旋转排序数组中的最小值

最小元素的位置以旋转次数为索引的位置&#xff0c;但是没有告诉旋转次数&#xff0c;换一种思路 当遇到arr[index] > arr[index1]时&#xff0c;index1为最小元素的位置。首位位置独立比较。但是这种方法还是遍历数组 观察两组数的中间值与首尾的值&#xff0c;又由于数组…

变现:利用 chatgpt + midjourney 制作微信表情包

1、利用gpt生成提示词&#xff0c;当然也可以直接翻译 生成基础提示词&#xff0c; 比如&#xff1a; an anime image with a white kawaii character in it, in the style of light green and brown, minimalist detail, animated gifs, cranberrycore, 1860–1969, babyco…

企业做数字化转型,请先避开这5个坑!

前言&#xff1a; “多种薪酬结构并存”的中国企业&#xff0c;在面临线下算薪、线上算税、表格数据整理易出错的问题时&#xff0c;有没有高效的解决办法&#xff1f;在考勤记录需要结合纸质版假条核对时&#xff0c;怎样减少人事的工作量&#xff1f;企业在积累了海量业务数…

【探讨】Java POI 处理 Excel 中的名称管理器

前言 最近遇到了一些导表的问题。原本的导表工具导不了使用名称管理器的Excel。 首先我们有两个Sheet。B1用的是名称管理器中的AAA, 而B2用的对应的公式。 第二个sheet&#xff0c;名为Test2: 这是一段简化的代码&#xff1a; public class Main {public static void mai…

Rust ESP32C3开发

Rust ESP32C3开发 系统开发逐步使用Rust语言&#xff0c;在嵌入式领域Rust也逐步完善&#xff0c;本着学习Rust和ESP32的目的&#xff0c;搭建了ESP32C3的环境&#xff0c;过程中遇到了不少问题&#xff0c;予以记录。 ESP-IDF开发ESP32 这一部分可跳过&#xff0c;是使用C开…

浏览器中的Markdown编辑器StackEdit

目前博客的 Pageviews 大约是之前的 10%&#xff0c;而 Uniques 则大约是 15% 左右。看来很多同学已经彻底迷路了 大家可以关注CSDN&#xff0c;地址&#xff1a; https://blog.csdn.net/wbsu2004 微信公众号也可以关注起来 什么是 StackEdit &#xff1f; StackEdit 是基于 P…

pyspark 笔记 cast 转换列的类型

1 不借助 pyspark.sql.types from pyspark.sql.functions import coldata [("Alice", "28"), ("Bob", "22"), ("Charlie", "30")] columns ["name", "age_str"] df spark.createDataFram…

如何使用vscode连接远程服务器

1、安装remote-ssh 在应用商店搜索remote-ssh&#xff0c;安装remote-ssh 2、安装完成后会出现远程资源管理器 3、点击远程资源管理器 --ssh的➕号&#xff0c;在输出框内输入要连接的服务器ip及账户名 如&#xff1a;ssh 账户名ip地址 4、输入后回车保存 5、保存后刷新一下 6…

SOLIDWORKS磁力配合工具

在我们平常的工作中&#xff0c;很多人都会面临大型装配体相互配合的问题。有很多用户&#xff0c;由于设计的产品体积很大&#xff0c;零件数量非常多。即使将设备分成不同的部件进行组装&#xff0c;不同的部件之间进行配合也非常困难。因为这种时候软件运行的速度会变得非常…