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. 绑定方法和隐藏属性之property装饰器

    目录 一:绑定方法 1.绑定给对象的方法 2.绑定给类的方法 3.非绑定方法之静态方法 二,隐藏属性 1.如何隐藏属性 三,property 装饰器 一:绑定方法 1.绑定给对象的方法 class P ...

  2. 「JOI 2014 Final」裁剪线

    做法一 首先将边界也视作四条裁剪线,整个平面作为一张纸,视存在 \(y = -\infty, y = +\infty, x = -\infty, x = +\infty\) 四条直线. 按照纵坐标依次 ...

  3. AT2402 [ARC072D] Dam

    首先我们可以将 \(t_i \times v_i\) 看作一个整体,不妨令 \(x_i = v_i, y_i = t_i \times v_i\) 这样两堆水混合后相当于将两个维度相加,方便了计算. ...

  4. 「SDWC2018 Day1」网格

    题目当中有三条限制,我们来逐一考虑.对于第一条限制,每次走动的增加量 \(x_i \le M_x, y_i \le M_y\),可以发现一共走的步数是确定的,那么就相当于解这样两个方程组: \[x_1 ...

  5. Atcoder ARC-064

    ARC064(2020.7.23) A 直接贪心即可. B 手玩样例可以猜出这样一个结论,如果两端字符相同,如果字符串长度为奇数那么后手赢,否则先手赢,两端字符不同则相反.证明的话先从特殊情况开始入手 ...

  6. C语言中数组长度不能用变量定义吗?

    C语言里,所有变量空间都是在程序运行之初向系统申请的,包括指针,除了一些动态申请方式如malloc函数.没有申请的空间系统是不允许读写的.那么如果一个数组大小是变量定义,则在程序运行前不能确定数组大小 ...

  7. aidl的应用场景

    支付宝 package com.tesy.alipay; import com.test.alipay.Iservice.Stub; import android.app.Service; impor ...

  8. @property修饰符

    @property修饰符 修饰是否生成getter方法的 readonly 只生成setter方法,不生成getter方法 readwrite 既生成getter 又生成setter方法(默认) @p ...

  9. Lab: Web shell upload via Content-Type restriction bypass

    首先上传一个正常头像. 之后,上传木马文件,并抓包 POST /my-account/avatar HTTP/1.1 Host: ac4f1f7d1eaa6cd2c0d80622001b00f9.we ...

  10. Spring-BeanFactory体系介绍

    1 BeanFactory介绍 BeanFactory是Spring中的根容器接口,所有的容器都从从它继承而来,ApplicationContext中对于BeanDefinition的注册,bean实 ...