提供时间日期的表达和相关函数。
QDateTime通过日期+时间来构造一个日期时间。它综合了QDate和QTime的所有特性。
它可以通过系统时钟来获取当前DateTime。它还提供了比较时间的方法和对时间进行加减运算的方法。
QDateTime可以分别通过本地时间、UTC、UTC偏移时间、指定时区(通过QTimeZone类实现)来描述日期时间。这些时间间是相关的,例如,UTC偏移 +3600×8 秒 等同于 ISO标准标识 UTC+08:00,等同于 时区为CN/Beijing 的时间。
一个QDateTime既可以直接通过传入一个date和time进行构造,也可以调用如currentDateTime()、fromMSecsSinceEpoch()的静态函数来进行构造。date和time可以通过setDate()、setTime()来进行设置,也可以直接通过setMSecsSinceEpoch()来用时间戳进行构造,也可以用fromString()来从一个String用相关的格式进行构造。
QDateTime::currentDateTime()返回当前DateTime,QDateTime::currentDateTimeUtc()返回UTC下的当前时间。
可以通过date()、time()来提取该DateTime中的Date与Time。同样的信息可以通过在toString()中传入指定格式来实现。
addMSecs():加上毫秒
addSecs():秒
addDays():日
addMonths():月
addYears():年
daysTo():两个DateTime间的天数
msecTo():两个DateTime间的毫秒数
toTimeSpec():转换为本地时间
toOffsetFromUtc():转换为UTC偏移时间
toTimeZone():转换为时区时间
timeSpec():存储时间的相对时间规范
Qt::TimeZone + timeZone():返回时区
只有-1与1年
Header:
#include
qmake:
QT += core
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
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
QDateTime
当前本地时间
当前UTC时间
qint64
当前时间戳(毫秒)
当前时间戳(秒)
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
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()
提取QDateTime的相关信息
qint64
daysTo(QDateTime other)
msecsTo(QDateTime other)
secsTo(QDateTime other)
两个QDateTime间的间隔,单位分别是天、毫秒、秒
bool
是否是夏令时
bool
isNull()
当Date和Time都是null时返回true
bool
isValid()
int
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()
toNSDate()
toOffsetFromUtc(int offsetSeconds)
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Ⓜ️s ap
2:13:9 pm
如果DateTime非法,会返回一个空QString。
正如dd与ddd,打印时分别是21和Tue,不同个数的格式字符打印出的内容也不同,有兴趣可以自行尝试。
格式用到的标准是ISO 8601。
QDateTime dt = QDateTime::currentDateTime();
dts = dt.toString("yyyy-MM-dd HH:mm:ss ddd");
//"2021-07-16 10:13:00 周五"
手机扫一扫
移动阅读更方便
你可能感兴趣的文章