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. 日志模块详细介绍 hashlib模块 动态加盐

    目录 一:hashlib模块 二:logging 一:hashlib模块 加密: 将明文数据通过一系列算法变成密文数据(目的就是为了数据的安全) 能够做文件一系列校验 python的hashlib提供 ...

  2. 微服务架构 | 4.2 基于 Feign 与 OpenFeign 的服务接口调用

    目录 前言 1. OpenFeign 基本知识 1.1 Feign 是什么 1.2 Feign 的出现解决了什么问题 1.3 Feign 与 OpenFeign 的区别与对比 2. 在服务消费者端开启 ...

  3. 《Effective TypeScript》条款22 - 类型收缩

    本文主要记录书中关于TypeScript类型收缩的内容 本文主要内容如下 类型收缩的一些方法 条件判断 抛错误 instanceof 和 in 属性检查 "标签联合"或" ...

  4. SpringBoot Log4j 安全漏洞分析及解决方案

    一.序言 SpringBoot作为Java基础框架大行其道,前不久爆发出Log4j安全漏洞,大众更多关心Log4j的危害是多么严重,然而鲜有关心SpringBoot这一底层框架的安全性问题,换而言之, ...

  5. 记一次.net core 异步线程设置超时时间

    前言: 刷帖子看到一篇 Go 记录一次groutine通信与context控制 看了一下需求背景,挺有意思的,琢磨了下.net core下的实现 需求背景: 项目中需要定期执行任务A来做一些辅助的工作 ...

  6. Java线程池实现原理及其在美团业务中的实践(转)

    转自美团技术团队:https://tech.meituan.com/2020/04/02/java-pooling-pratice-in-meituan.html 随着计算机行业的飞速发展,摩尔定律逐 ...

  7. OSChina 文

    转载请注明来源:https://www.cnblogs.com/hookjc/ http://www.oschina.net/p/carbon-forum   [高性能PHP论坛 Carbon For ...

  8. axios取消接口请求

    axios取消请求 这里就是分析一下接口请求需要被取消时的一些操作 因为我是用vue写的项目,所以标配用的是axios,怎么在axios中取消已经发送的请求呢? 1.在这之前我们还是先介绍一下原生js ...

  9. MySQL数据类型的最优选择

    MySQL数据类型的最优选择   慎重选择数据类型很重要.为啥哩?可以提高性能.原理如下:            ● 存储(内存.磁盘).从而节省I/O(检索相同数据情况下)      ● 计算.进而 ...

  10. finally块

    /* finally 块: finally块的 使用前提是必须要存在try块才能使用. finally块的代码在任何情况下都会执行的,除了jvm退出的情况. finally非常适合做资源释放的工作,这 ...