QCalendar
一、描述
QCalendar 对象使用特定系统的规则将年、月和日编号映射到特定日期(最终由其儒略日编号标识)。
二、类型成员
1、enum class QCalendar::System:此枚举类型用于指定日历系统的选择。
- Gregorian:默认日历,国际通用。
- Julian:古罗马日历。
- Milankovic:一些东正教教堂使用的修订版儒略历。
- Jalali:太阳回历(也称为波斯历)。
- IslamicCivil:伊斯兰民历。
三、成员函数
1、QDate dateFromParts(int year, int month, int day)
将年、月和日转换为 QDate。
2、[static] QStringList availableCalendars()
返回可用日历系统的名称列表。
3、int dayOfWeek(QDate date)
返回给定日期的星期几。1 - 7。如果日历无法表示指定的日期,则返回零。
4、int daysInMonth(int month, int year = Unspecified)
返回给定年份的给定月份中的天数。
5、int daysInYear(int year)
返回给定年份的天数。
6、bool isDateValid(int year, int month, int day)
如果给定的年、月和日在此日历中是否指定了有效日期。
7、bool isLeapYear(int year)
给定年份是否是闰年。
8、bool isLunar()
此日历是否是农历。
9、bool isLuniSolar()
此日历是否是阴阳历。阴阳历表达了月亮的相位,但会自行调整以跟踪太阳在天空中相对于固定恒星的不同位置。
10、bool isSolar()
此日历是否太阳历。太阳历主要基于太阳在天空中相对于固定恒星的不同位置。
QDate
一、有效日期范围
日期在内部存储为儒略日数,即连续范围内每一天的整数计数,公历中公元前 4714 年 11 月 24 日为儒略日 0(儒略历中公元前 4713 年 1 月 1 日)。它不仅是一种存储绝对日期的有效且准确的方式,还适用于将日期转换为其他日历系统,例如希伯来语、伊斯兰或中文。
由于技术原因,QDate 可以表示的儒略日数字范围限制在 -784350574879 和 784354017364 之间,这意味着从公元前 20 亿年前到公元 20 亿年后。这是 QDateTime 可以表示的日期范围的七倍多。
二、成员函数
1、QString toString(const QString &format, QCalendar cal = QCalendar())
以字符串形式返回日期。format 参数确定结果字符串的格式。如果提供了 cal,则它确定用于表示日期的日历,默认为公历。
- d:作为没有前导零的数字的日期(1 到 31)
- dd:作为带前导零的数字的日期(01 到 31)
- ddd:缩写的日期名称('Mon' 到 'Sun')
- dddd:长日名称('Monday' 到 'Sunday')
- M:没有前导零的数字形式的月份(1 到 12)
- MM:月份为带前导零的数字(01 到 12)
- MMM:缩写的月份名称(“Jan”到“Dec”)
- MMMM:长月份名称(“一月”到“十二月”)
- yy:两位数的年份(00 到 99)
- yyyy:四位数的年份。
2、QDate addDays(qint64 ndays)
返回的 QDate 对象包含比此对象的日期晚 n 天的日期(如果 ndays 为负,则早n天)。
3、QDate addMonths(int nmonths, QCalendar cal)
返回的QDate 对象包含比此对象的日期晚 n 个月的日期(如果 nmonths 为负,则早n月)。
4、QDate addYears(int nyears, QCalendar cal) / QDate addYears(int nyears)
返回的QDate 对象包含比此对象的日期晚 n 年的日期(如果 nyears 为负,则早n年)。
5、[static] QDate currentDate()
返回系统时钟报告的当前日期。
6、int dayOfWeek(QCalendar cal) / int dayOfWeek()
返回此日期是星期几(1 = 星期一到 7 = 星期日)。
7、int dayOfYear(QCalendar cal) / int dayOfYear()
返回此日期是一年中的第几天。
8、int daysInMonth(QCalendar cal) / int daysInMonth()
返回此日期所属月份中的天数。
9、int daysInYear(QCalendar cal) / int daysInYear()
返回此日期所属的年份的天数。
10、qint64 daysTo(QDate d)
返回从当前日期到 d 的天数(如果 d 早于当前日期,则返回负数)。
QDate d1(2021, 5, 17);
QDate d2(2021, 5, 20);
d1.daysTo(d2); // returns 3
d2.daysTo(d1); // returns -3
11、[static] QDate fromJulianDay(qint64 jd) / qint64 toJulianDay()
儒略日与QDate相互转换。
12、[static] QDate fromString(const QString &string, const QString &format, QCalendar cal = QCalendar())
使用给定的格式返回由字符串表示的 QDate,如果无法解析字符串,则返回无效日期。
qDebug()<< QDate::fromString("1.30", "M.d");
qDebug()<< QDate::fromString("20000110", "yyyyMMdd");
qDebug()<< QDate::fromString("20000110", "yyyyMd");
qDebug()<< QDate::fromString("2021-09-21","yyyy-MM-dd");
qDebug()<< QDate::fromString("2021测09试21","yyyy测MM试dd");
13、void getDate(int *year, int *month, int *day)
提取日期的年、月和日,并将它们分配给 *year、*month 和 *day。
14、[static] bool isLeapYear(int year)
如果指定年份是否公历中的闰年。
15、bool isNull()
此函数的行为等效于 isValid()。
16、bool isValid() / [static] bool isValid(int year, int month, int day)
是否有效 / 指定的日期(年、月和日)在公历中是否有效。
17、int month(QCalendar cal)
返回日历对象 ca 的月数。
18、QString toString(Qt::DateFormat format = Qt::TextDate)
以字符串形式返回日期。format 参数确定字符串的格式。
qDebug()<< QDate::currentDate().toString(Qt::TextDate);
qDebug()<< QDate::currentDate().toString(Qt::ISODate);
qDebug()<< QDate::currentDate().toString(Qt::RFC2822Date);
qDebug()<< QDate::currentDate().toString(Qt::ISODateWithMs);
qDebug()<< QLocale::system().toString(QDate::currentDate());
19、int weekNumber(int *yearNumber = nullptr)
返回 ISO 8601 周数(1 到 53)。(当前日期是一年第几周)
如果 yearNumber 不是 nullptr 则将年份存储为 *yearNumber。
注意:*yearNumber 并不总是与 year() 相同。例如,2000 年 1 月 1 日在 1999 年的第 52 周,2002 年 12 月 31 日在 2003 年的第 1 周。
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 为单位。
免费学习C++ Qt开发教程视频,点击下面链接免费报名领取视频学习资料
C/C++项目实战/Qt5/C语言/c++/数据库/OpenCV/MFC/QT项目-学习视频教程-腾讯课堂
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
免费学习C++ Qt开发教程视频,点击下面链接免费报名领取视频学习资料
C/C++项目实战/Qt5/C语言/c++/数据库/OpenCV/MFC/QT项目-学习视频教程-腾讯课堂
原文链接:https://blog.csdn.net/kenfan1647/article/details/120399743