ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

QDateTime

2021-09-21 19:32:29  阅读:302  来源: 互联网

标签:Qt qint64 日期 时间 static QDateTime


一、描述

QDateTime 对象对日历日期和时钟时间进行编码。它结合了 QDate QTime 类的特性。它可以从系统时钟中读取当前日期时间。它提供了用于比较日期时间和通过添加秒数、天数、月数或年数来操作日期时间的函数。

二、成员函数

1、QString toString(const QString &format, QCalendar cal = QCalendar()) 

以字符串形式返回日期时间。format 参数确定结果字符串的格式。

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

2、QDateTime addDays(qint64 ndays) 

      QDateTime addMSecs(qint64 msecs)

      QDateTime addMonths(int nmonths) 

      QDateTime addSecs(qint64 s) 

      QDateTime addYears(int nyears) 

返回的对象包含比此对象的日期时间晚 n 天 / msecs 毫秒 / nmonths 个月 / s 秒 / n 年的日期时间(如果参数为负,则更早)。

3、[static] QDateTime currentDateTime()

返回本地时区中系统时钟报告的当前日期时间。

4、[static] QDateTime currentDateTimeUtc()

返回系统时钟报告的当前日期时间,以 UTC 为单位。

 5、[static] qint64 currentMSecsSinceEpoch()

返回自 1970-01-01T00:00:00 通用协调时间以来的毫秒数。

6、[static] qint64 currentSecsSinceEpoch()

返回自 1970-01-01T00:00:00 通用协调时间以来的秒数。

7、QDate date() / QTime time()

返回日期时间的日期 / 时间部分。

8、qint64 daysTo(const QDateTime &other) 

返回从此日期时间到另一个日期时间的天数。

天数计算为在此日期时间到另一个日期时间之间到达午夜的次数。即着从 23:55 到第二天 0:05 的 10 分钟差异计为一天。

如果其他日期时间早于此日期时间,则返回值为负。

       qint64 msecsTo(const QDateTime &other) 

       qint64 secsTo(const QDateTime &other)

返回从此日期时间到另一个日期时间的毫秒数 / 秒数。如果 other 日期时间早于此日期时间,则返回值为负。如果任一日期时间无效,则返回 0。

9、[static] QDateTime fromMSecsSinceEpoch(qint64 msecs,Qt::TimeSpec spec = Qt::LocalTime,int offsetSeconds = 0)

     [static] QDateTime fromMSecsSinceEpoch(qint64 msecs,const QTimeZone &timeZone)

返回一个日期时间,其日期和时间是自 1970-01-01T00:00:00.000,协调世界时 (Qt::UTC) 以来经过并转换为给定规范的毫秒数。

10、[static] QDateTime fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec = Qt::LocalTime, int offsetSeconds = 0)

        [static] QDateTime fromSecsSinceEpoch(qint64 secs, const QTimeZone &timeZone)

同上,秒。

11、[static] QDateTime fromString(const QString &string, Qt::DateFormat format = Qt::TextDate)

       [static] QDateTime fromString(const QString &string, const QString &format, QCalendar cal = QCalendar())

使用给定的格式返回由字符串表示的QDateTime。

    qDebug()<<QDateTime::fromString("2021-09-21 18:30:14.699","yyyy-MM-dd hh:mm:ss.zzz");

12、bool isDaylightTime()

此日期时间是否属于夏令时。

13、bool isNull() 

日期和时间是否都为空。空日期时间无效。

14、bool isValid()

日期和时间是否都有效。

15、void setDate(QDate date)

将此日期时间的日期部分设置为date。如果尚未设置时间,则设置为午夜。如果日期无效,则此 QDateTime 无效。 

       void setTime(QTime time)

将此日期时间的时间部分设置为time。如果时间无效,此函数将其设置为午夜。

可以通过将 QDateTime 设置为默认 QTime 来清除 QDateTime 中的任何设置时间:

 QDateTime dt = QDateTime::currentDateTime();
 dt.setTime(QTime());

16、void setMSecsSinceEpoch(qint64 msecs) / void setSecsSinceEpoch(qint64 secs)

根据自 1970-01-01T00:00:00.000,协调世界时 (Qt::UTC) 以来经过的毫秒数 / 秒数设置日期和时间。

qint64 toMSecsSinceEpoch() / qint64 toSecsSinceEpoch() 

17、void setTimeSpec(Qt::TimeSpec spec)

设置此日期时间中使用的时间规范。

enum Qt::TimeSpec

  • Qt::LocalTime:Local 时间,由系统时区设置控制。
  • Qt::UTC:协调世界时。
  • Qt::OffsetFromUTC:从协调世界时以秒为单位的偏移量。
  • Qt::TimeZone

标签:Qt,qint64,日期,时间,static,QDateTime
来源: https://blog.csdn.net/kenfan1647/article/details/120399743

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有