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. html图像 表格 列表

    创建图像映射 <img src="/demo/planets.gif" width="145" height="126" alt=&q ...

  2. *衡树 Treap(树堆) 学习笔记

    调了好几个月的 Treap 今天终于调通了,特意写篇博客来纪念一下. 0. Treap 的含义及用途 在算法竞赛中很多题目要使用二叉搜索树维护信息.然而毒瘤数据可能让二叉搜索树退化成链,这时就需要让二 ...

  3. Vue.js之计算属性(computed)、属性监听(watch)与方法选项(methods)

    vue.js官网:https://cn.vuejs.org/v2/guide/components-registration.html 一.计算属性-computed 1. 作用:能够避免数据冗余,通 ...

  4. springboot 使用select的注解,来查询数据库。

    package com.aaa.zxf.mapper; import com.aaa.zxf.model.Book; import org.apache.ibatis.annotations.*; i ...

  5. git merge -ff --no-ff --squash 的区别

    感谢原文作者:futureme 原文链接:https://www.cnblogs.com/taylorluo/articles/10810762.html git merge #没有参数(默认为–ff ...

  6. Linux添加用户组和添加用户

    1.用户组 添加组:groupadd 组名 [root@Server-n93yom ~]# groupadd dev [root@Server-n93yom ~]# cat /etc/group | ...

  7. 开源项目(asyncHttpClient) get post 方式提交

    get方式: 1 public static void requestNetForGetLogin(final Context context,final Handler handler ,final ...

  8. jdk1.5出现的新特性---->增强for循环

    import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; ...

  9. JMM之Java中锁概念的分类总结

    在Java的并发编程中不可避免的涉及到锁.从不同维护可以将锁进行不同的分类,如下: 1.乐观锁和悲观锁(根据读写操作的比例划分) 乐观锁是一种乐观思想,即认为读多写少,遇到并发写的可能性低,每次去拿数 ...

  10. 【一天一个小知识10/20】Unity通过www获取json文本信息。

    前提:领导要我在unity获取局域网服务器的文本信息.给了一个json的网络文本让我测试.我对于json以及服务器比较陌生.就直接去网上找相关的资料. 以下是自己测试的代码,没问题. 测试的网络jso ...