0、说明

提供时间日期的表达和相关函数。

QDateTime通过日期+时间来构造一个日期时间。它综合了QDate和QTime的所有特性。

它可以通过系统时钟来获取当前DateTime。它还提供了比较时间的方法和对时间进行加减运算的方法。

QDateTime可以分别通过本地时间、UTC、UTC偏移时间、指定时区(通过QTimeZone类实现)来描述日期时间。这些时间间是相关的,例如,UTC偏移 +3600×8 秒 等同于 ISO标准标识 UTC+08:00,等同于 时区为CN/Beijing 的时间。

1)构造

一个QDateTime既可以直接通过传入一个date和time进行构造,也可以调用如currentDateTime()、fromMSecsSinceEpoch()的静态函数来进行构造。date和time可以通过setDate()、setTime()来进行设置,也可以直接通过setMSecsSinceEpoch()来用时间戳进行构造,也可以用fromString()来从一个String用相关的格式进行构造。

2)当前时间

QDateTime::currentDateTime()返回当前DateTime,QDateTime::currentDateTimeUtc()返回UTC下的当前时间。

3)提取与设置

可以通过date()、time()来提取该DateTime中的Date与Time。同样的信息可以通过在toString()中传入指定格式来实现。

4)运算

addMSecs():加上毫秒

addSecs():秒

addDays():日

addMonths():月

addYears():年

daysTo():两个DateTime间的天数

msecTo():两个DateTime间的毫秒数

5)转换

toTimeSpec():转换为本地时间

toOffsetFromUtc():转换为UTC偏移时间

toTimeZone():转换为时区时间

timeSpec():存储时间的相对时间规范

Qt::TimeZone + timeZone():返回时区

注意事项

①没有0年

只有-1与1年

1、模块和加载项

Header: #include <QDateTime>
qmake: QT += core

2、构造

QDateTime(QDateTime other) 构造另一个QDateTime的副本
QDateTime(QDate date, QTime time, QTimeZone timeZone) 用指定的Date、Time、TimeZone构造DateTime
QDateTime(QDate date, QTime time, Qt::TimeSpec spec, int offsetSeconds)  
QDateTime(QDate date, QTime time, Qt::TimeSpec spec = Qt::LocalTime)  
QDateTime(QDate date) 用指定QDate构造QDateTime
QDateTime()  

3、静态字段

enum class YearRange { First, Last } 年的范围 
Constant Value Description
QDateTime::YearRange::First -292275056 The later parts of this year are representable
QDateTime::YearRange::Last +292278994 The earlier parts of this year are representable

5、静态方法

返回值类型

方法

说明

QDateTime  currentDateTime() 当前本地时间
currentDateTimeUtc() 当前UTC时间 
qint64 currentMSecsSinceEpoch() 当前时间戳(毫秒)
currentSecsSinceEpoch() 当前时间戳(秒) 
QDateTime fromCFDate(CFDateRef date) 从一个CFDate构造QDateTime

fromMSecsSinceEpoch(qint64 msecs)

fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int offsetSeconds = 0)

fromMSecsSinceEpoch(qint64 msecs, QTimeZone timeZone)

返回时间戳(毫秒)对应的QDateTime
fromNSDate(const NSDate *date) 从NSDate构造QDateTime

fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec = Qt::LocalTime, int offsetSeconds = 0)

fromSecsSinceEpoch(qint64 secs, QTimeZone timeZone)

返回时间戳(秒)对应的QDateTime

fromString(QString string, Qt::DateFormat format = Qt::TextDate)

fromString(QString string, QString format)

fromString(QString string, QString format, QCalendar cal)

从QString用指定格式构造QDateTime

6、实例方法

返回值类型

方法

说明

QDateTime &

bool

bool

bool

bool

bool

bool

operator=(QDateTime other)

operator!=(QDateTime other)

operator<(QDateTime other)

operator<=(QDateTime other)

operator==(QDateTime other)

operator>(QDateTime other)

operator>=(QDateTime other)

赋值

比较两个日期的大小

QDateTime

addDays(qint64 ndays)

addMSecs(qint64 msecs)

addMonths(int nmonths)

addSecs(qint64 s)

addYears(int nyears)

QDateTime加一定时间,得到新QDateTime。

如果参数是负数就表示减一定时间。

QDate

QTime

Qt::TimeSpec

QTimeZone

QString

date()

time()

timeSpec()

timeZone()

timeZoneAbbreviation()

提取QDateTime的相关信息

qint64

daysTo(QDateTime other)

msecsTo(QDateTime other)

secsTo(QDateTime other)

两个QDateTime间的间隔,单位分别是天、毫秒、秒
bool isDaylightTime()   是否是夏令时
bool   isNull() 当Date和Time都是null时返回true
bool   isValid()  
int  offsetFromUtc() UTC偏移,单位秒
void

setDate(QDate date)

setMSecsSinceEpoch(qint64 msecs)

setOffsetFromUtc(int offsetSeconds)

setSecsSinceEpoch(qint64 secs)

setTime(QTime time)

setTimeSpec(Qt::TimeSpec spec)

setTimeZone(QTimeZone toZone)

设置QDateTime的相关信息
 void  swap(QDateTime &other) 互换两个QDateTime的相关信息

CFDateRef

QDateTime

qint64

NSDate *

QDateTime

qint64

QDateTime

QDateTime

QDateTime

toCFDate()

toLocalTime()

toMSecsSinceEpoch()

toNSDate()

toOffsetFromUtc(int offsetSeconds)

toSecsSinceEpoch()

toTimeSpec(Qt::TimeSpec spec)

toTimeZone(QTimeZone timeZone)

toUTC()

把QDateTime转换为对应的时间
QString

toString(QString format)

toString(Qt::DateFormat format = Qt::TextDate)

toString(QString format, QCalendar cal)

返回该DateTime对应的QString,参数format指定了转化的格式。

假设QDateTime是 21 May 2001 14:13:09.120

相关转化格式为:

Format Result
dd.MM.yyyy 21.05.2001
ddd MMMM d yy Tue May 21 01
hh:mm:ss.zzz 14:13:09.120
hh:mm:ss.z 14:13:09.12
h:m:s ap 2:13:9 pm

如果DateTime非法,会返回一个空QString。

正如dd与ddd,打印时分别是21和Tue,不同个数的格式字符打印出的内容也不同,有兴趣可以自行尝试。

格式用到的标准是ISO 8601。

7、一些用法

1)获取当前日期

QDateTime dt = QDateTime::currentDateTime();

2)转换为String

dts = dt.toString("yyyy-MM-dd HH:mm:ss ddd");
//"2021-07-16 10:13:00 周五"

Qt:QDateTime的更多相关文章

  1. Qt:QDateTime、QDate、QTime与QDateTimeEdit

    时间日期是经常遇到的数据类型,Qt中的时间日期类如下: QTime:时间类型,只表示时间,如15:23:13: QDate:日期类型,只表示日期,如2017-4-5: QDateTime:日期时间类型 ...

  2. Qt:禁止qDebug的输出

    Qt:禁止qDebug的输出 在工程的.pro文件里加上以下编译批令即可: DEFINES += QT_NO_DEBUG_OUTPUT

  3. Qt:使用自定义的字体

    Qt:使用自定义的字体 1. 下载字体文件 2. 加载字体文件 3. 使用字体   QFontDatabase::addApplicationFont("XENOTRON.TTF" ...

  4. Qt:QJsonObject

    0.说明 QJsonObject在逻辑上就是一个Map或Dict!记住这一点对理解它的方法.说明很有帮助. QJsonObject类封装了JSON Object. JSON Object是一个Key- ...

  5. Qt:QJsonValue

    0.说明 QJsonValue类用于操作JSON中的各种数据. JSON是用于存储结构化数据的格式,JSON中的数据可以是六种类型: 基本类型 存储类型 bool QJsonValue::Bool d ...

  6. Qt:QJsonArray

    0.说明 QJsonArray中存储了一系列的QJsonValue.可以向其中插入.删除QJsonValue. 一个QJsonArray可以与QVariantList互相转换.可以通过size()访问 ...

  7. Qt:QUrl构造时的qrc前缀

    参考(按对我帮助从大到小排列): Qt内的各种路径(让人迷惑) - 鬼谷子com - 博客园 qt webengineview 加载本地资源方式 - beautifulday - 博客园 (17条消息 ...

  8. QT:“下载速度柱状图”的模拟实现——思路真好,会动脑筋,连我都有了启发(这个思路好像是通用的)

    不知是哪个版本的迅雷,有个“下载速度柱状图”的小界面,我比较喜欢(只不过最新版本的迅雷却没了),所以决定来山寨一个.当然,这个山寨品不能下载文件,呵呵. 思路:1:将界面的背景涂成黑色2:每隔0.1秒 ...

  9. Qt:基于TCP和UDP的局域网P2P(局域网)通讯封装

    封装了一个类,可以进行在局域网进行P2P通讯(仅局域网可用) 也就是说,假设局域网中有10台电脑,那么从本机发出的数据,将依次派发到这10台电脑(目前的设计中包括自己这台) 在使用方面,构造的时候给端 ...

随机推荐

  1. 信奥题库(OI题库)8月月赛T1题解 幂次数

    0.前置知识 分解质因数 快速幂(不必要) 1.思路 首先,我们知道一个正整数(设它为 \(a\) )一定能分解成这样的形式: \[a= \prod_{i\in N^*} p_i^{c_i} \] 其 ...

  2. AtCoder Beginner Contest 220 A-F

    A #include <iostream> using namespace std; int main() { int a, b, c; cin >> a >> b ...

  3. JavaCV的摄像头实战之五:推流

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本文是<JavaCV的摄像头实战> ...

  4. C语言之清空缓存区

    感谢大佬:https://blog.csdn.net/qq_26768741/article/details/50933598 在C语言中,我们常常需要去清空缓存区,对于缓存区清空的重要性,接下来我们 ...

  5. mac不能用ip访问项目的, 还有80端口不能开启的问题

    开启80端口 参考这篇文章 解决mac无法使用80端口问题,亲测可用 1. 防火墙设置问题 2. 网卡问题 3. 服务器地址配置问题

  6. DockerClient端与DockerDaemon的通信安全

    DockerClient端与DockerDaemon的通信安全 容器的安全性问题的根源在于容器和宿主机共享内核.如果容器里的应用导致Linux内核崩溃,那么整个系统可能都会崩溃.与虚拟机是不同的,虚拟 ...

  7. LeetCode随缘刷题之回文数

    package leetcode.day_01_30; /** * 给你一个整数 x ,如果 x 是一个回文整数,返回 true :否则,返回 false . * <p> * 回文数是指正 ...

  8. SQL——with as 临时表

    一.WITH AS的含义    WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候,是 ...

  9. k8s笔记--驱逐与重调度,以及deschueduler的一次实验

    在Kubernetes中,调度是指将Pod放置到合适的Node上,然后对应的Node上的Kubelet才能够运行这些pod.调度器通过Kubernetes的监测机制来发现集群中新创建且尚未被调度的No ...

  10. 从 MMU 看内存管理

    在计算机早期的时候,计算机是无法将大于内存大小的应用装入内存的,因为计算机读写应用数据是直接通过总线来对内存进行直接操作的,对于写操作来说,计算机会直接将地址写入内存:对于读操作来说,计算机会直接读取 ...