6.0.0-rc4Source

QDateTime Class

The QDateTime class provides date and time functions. More…

Header#include <QDateTime>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Functions

QDateTime()
QDateTime(QDate date, QTime time, Qt::TimeSpec spec = Qt::LocalTime, int offsetSeconds = 0)
QDateTime(QDate date, QTime time, const QTimeZone &timeZone)
boolisNull() const
boolisValid() const
QDatedate() const
QTimetime() const
Qt::TimeSpectimeSpec() const
intoffsetFromUtc() const
qint64toMSecsSinceEpoch() const
qint64toSecsSinceEpoch() const
voidsetDate(QDate date)
voidsetTime(QTime time)
voidsetTimeSpec(Qt::TimeSpec spec)
voidsetOffsetFromUtc(int offsetSeconds)
QTimeZonetimeZone() const
voidsetTimeZone(const QTimeZone &toZone)
QDateTimetoTimeZone(const QTimeZone &toZone) const
boolisDaylightTime() const
QStringtimeZoneAbbreviation() const
voidswap(QDateTime &other)
voidsetMSecsSinceEpoch(qint64 msecs)
voidsetSecsSinceEpoch(qint64 secs)
QDateTimeaddDays(qint64 days) const
QDateTimeaddMonths(int months) const
QDateTimeaddYears(int years) const
QDateTimeaddSecs(qint64 secs) const
QDateTimeaddMSecs(qint64 msecs) const
qint64daysTo(const QDateTime &o) const
qint64secsTo(const QDateTime &o) const
qint64msecsTo(const QDateTime &o) const
QDateTimetoTimeSpec(Qt::TimeSpec spec) const
QDateTimetoLocalTime() const
QDateTimetoUTC() const
QDateTimetoOffsetFromUtc(int offsetSeconds) const
QStringtoString(Qt::DateFormat format = Qt::TextDate) const
QStringtoString(const QString &format) const

Static Public Members

QDateTimecurrentDateTime()
QDateTimecurrentDateTime(const QTimeZone &zone)
QDateTimecurrentDateTimeUtc()
qint64currentMSecsSinceEpoch()
qint64currentSecsSinceEpoch()
QDateTimefromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec = Qt::LocalTime, int offsetFromUtc = 0)
QDateTimefromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec = Qt::LocalTime, int offsetFromUtc = 0)
QDateTimefromMSecsSinceEpoch(qint64 msecs, const QTimeZone &timeZone)
QDateTimefromSecsSinceEpoch(qint64 secs, const QTimeZone &timeZone)
QDateTimefromString(const QString &string, Qt::DateFormat format = Qt::TextDate)
QDateTimefromString(const QString &string, const QString &format)

Detailed Description

A QDateTime combines a QDate and a QTime with a time specification: local time, UTC, or a fixed offset from UTC. It converts to and from milliseconds since the Unix epoch, 1970-01-01T00:00:00 UTC.

QDateTime now = QDateTime::currentDateTimeUtc();
QDateTime later = now.addDays(7);
later.toString(Qt::ISODate); // "2026-11-14T10:10:14Z"

See also QDate and QTime.

Differences from Qt

Local time follows the system's rules through mktime(); zones given as a QTimeZone use Nt's reader of the time zone database. A time that falls in a daylight-saving gap is moved forward, and in a fold the earlier time is chosen. There is no fromTime_t() and no TransitionResolution argument. Text formats use English text and the format string accepts the QDate and QTime expressions plus t for the time zone.

On Zephyr local time is UTC, as the C library has no time zones. See Nt on Zephyr.

Member Function Documentation

QDateTime::QDateTime()

Constructs a null datetime, which is invalid.

See also isNull() and isValid().

QDateTime::QDateTime(QDate date, QTime time, Qt::TimeSpec spec = Qt::LocalTime, int offsetSeconds = 0)

Constructs a datetime from date and time, interpreted according to spec. For Qt::OffsetFromUTC, offsetSeconds is the offset from UTC in seconds; an offset of 0 makes the datetime UTC. If date is valid and time is null, the time is taken as midnight.

See also isValid().

QDateTime::QDateTime(QDate date, QTime time, const QTimeZone &timeZone)

Constructs a datetime from date and time in timeZone. A zone for local time, UTC or a fixed offset gives the matching time specification; any other zone gives Qt::TimeZone. The datetime is invalid if the zone is invalid. A time that does not exist in the zone, because of a daylight-saving gap, is moved forward by the length of the gap.

This function was introduced in 5.2.

See also timeZone() and toTimeZone().

QDateTime QDateTime::addDays(qint64 days) const

Returns a QDateTime that is days days after this one, or before it if days is negative. The time and time specification are kept; the date changes as QDate::addDays() does.

See also addMonths(), addYears(), addSecs() and daysTo().

QDateTime QDateTime::addMonths(int months) const

Returns a QDateTime that is months months after this one, or before it if months is negative. The day is clamped as QDate::addMonths() does.

See also addDays() and addYears().

QDateTime QDateTime::addMSecs(qint64 msecs) const

Returns a QDateTime that is msecs milliseconds after this one, or before it if msecs is negative, in the same time specification. Returns an invalid QDateTime if this one is invalid.

See also addSecs() and msecsTo().

QDateTime QDateTime::addSecs(qint64 secs) const

Returns a QDateTime that is secs seconds after this one, or before it if secs is negative.

See also addMSecs() and secsTo().

QDateTime QDateTime::addYears(int years) const

Returns a QDateTime that is years years after this one, or before it if years is negative. The day is clamped as QDate::addYears() does.

See also addDays() and addMonths().

[static] QDateTime QDateTime::currentDateTime()

Returns the current datetime in local time.

See also currentDateTimeUtc(), QDate::currentDate() and QTime::currentTime().

[static] QDateTime QDateTime::currentDateTime(const QTimeZone &zone)

Returns the current date and time in zone.

This function was introduced in 5.2.

[static] QDateTime QDateTime::currentDateTimeUtc()

Returns the current datetime in UTC.

See also currentDateTime().

[static] qint64 QDateTime::currentMSecsSinceEpoch()

Returns the current time in milliseconds since the Unix epoch, as reported by the system clock.

See also currentSecsSinceEpoch() and currentDateTime().

[static] qint64 QDateTime::currentSecsSinceEpoch()

Returns the current time in seconds since the Unix epoch, as reported by the system clock.

See also currentMSecsSinceEpoch().

QDate QDateTime::date() const

Returns the date part of the datetime.

See also time() and setDate().

qint64 QDateTime::daysTo(const QDateTime &o) const

Returns the number of days from this datetime's date to the date of o, which is negative if o is earlier. The times are not taken into account. Returns 0 if either date is invalid.

See also secsTo() and addDays().

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

Returns a datetime for msecs milliseconds since the Unix epoch, shown in the time specification spec. For Qt::OffsetFromUTC, offsetFromUtc is the offset in seconds.

See also toMSecsSinceEpoch() and fromSecsSinceEpoch().

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

Returns the datetime msecs milliseconds after the Unix epoch, shown in timeZone. Returns an invalid datetime if the zone is invalid.

This function was introduced in 5.2.

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

Returns a datetime for secs seconds since the Unix epoch, shown in the time specification spec. For Qt::OffsetFromUTC, offsetFromUtc is the offset in seconds.

See also toSecsSinceEpoch() and fromMSecsSinceEpoch().

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

Returns the datetime secs seconds after the Unix epoch, shown in timeZone.

This function was introduced in 5.8.

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

Parses string in the given format and returns the datetime. Returns an invalid QDateTime if string does not match. ISO 8601 text may use a space instead of T, omit the time (which then means midnight), and end with Z or an offset; without either it is local time. RFC2822Date accepts an optional leading weekday, optional seconds, and a zone of GMT, UT, UTC, Z or +hhmm. TextDate expects ddd MMM d HH:mm:ss yyyy, optionally followed by GMT and an offset.

See also toString().

[static] QDateTime QDateTime::fromString(const QString &string, const QString &format)

This is an overloaded function.

Parses string using format, which uses the expressions of QDate and QTime, and returns the datetime in local time. Returns an invalid QDateTime if string does not match format.

See also toString().

bool QDateTime::isDaylightTime() const

Returns true if daylight-saving time is in effect at this datetime; otherwise returns false. UTC and fixed offsets have no daylight time.

This function was introduced in 5.2.

bool QDateTime::isNull() const

Returns true if both the date and the time are null; otherwise returns false.

See also isValid().

bool QDateTime::isValid() const

Returns true if both the date and the time are valid; otherwise returns false.

See also isNull().

qint64 QDateTime::msecsTo(const QDateTime &o) const

Returns the number of milliseconds from this datetime to o, which is negative if o is earlier. The difference is taken between the instants the two datetimes represent, so their time specifications do not matter.

See also secsTo() and addMSecs().

int QDateTime::offsetFromUtc() const

Returns the datetime's offset from UTC in seconds. It is 0 for UTC, the stored offset for Qt::OffsetFromUTC, and the system's offset at that moment for local time. Returns 0 for an invalid local datetime.

See also setOffsetFromUtc() and timeSpec().

qint64 QDateTime::secsTo(const QDateTime &o) const

Returns the number of whole seconds from this datetime to o, which is negative if o is earlier. The difference is taken between the instants the two datetimes represent, so their time specifications do not matter.

See also msecsTo() and addSecs().

void QDateTime::setDate(QDate date)

Sets the date part to date. If the time is not valid, it is set to midnight.

See also date() and setTime().

void QDateTime::setMSecsSinceEpoch(qint64 msecs)

Sets the datetime to msecs milliseconds since the Unix epoch, shown in the datetime's current time specification.

See also toMSecsSinceEpoch() and setSecsSinceEpoch().

void QDateTime::setOffsetFromUtc(int offsetSeconds)

Sets the time specification to a fixed offset of offsetSeconds from UTC, keeping the date and time fields as they are. An offset of 0 sets the specification to UTC.

See also offsetFromUtc() and toOffsetFromUtc().

void QDateTime::setSecsSinceEpoch(qint64 secs)

Sets the datetime to secs seconds since the Unix epoch, shown in the datetime's current time specification.

See also toSecsSinceEpoch() and setMSecsSinceEpoch().

void QDateTime::setTime(QTime time)

Sets the time part to time.

See also time() and setDate().

void QDateTime::setTimeSpec(Qt::TimeSpec spec)

Sets the time specification to spec, keeping the date and time fields as they are. The offset is reset to 0 unless spec is Qt::OffsetFromUTC; use setOffsetFromUtc() to set a fixed offset.

See also timeSpec() and toTimeSpec().

void QDateTime::setTimeZone(const QTimeZone &toZone)

Sets the time zone of the datetime to toZone, keeping the date and time fields, so the datetime then refers to a different instant.

This function was introduced in 5.2.

See also timeZone() and toTimeZone().

void QDateTime::swap(QDateTime &other)

Swaps this datetime with other.

This function was introduced in 5.0.

QTime QDateTime::time() const

Returns the time part of the datetime.

See also date() and setTime().

Qt::TimeSpec QDateTime::timeSpec() const

Returns the time specification of the datetime.

See also setTimeSpec() and toTimeSpec().

QTimeZone QDateTime::timeZone() const

Returns the time zone of the datetime: the zone given to it, or a zone for local time, UTC or the fixed offset of its time specification.

This function was introduced in 5.2.

See also setTimeZone() and timeSpec().

QString QDateTime::timeZoneAbbreviation() const

Returns the abbreviation of the time zone at this datetime, such as "CEST", "UTC" or "UTC+05:30" for a fixed offset.

This function was introduced in 5.2.

QDateTime QDateTime::toLocalTime() const

Returns a copy of this datetime converted to local time.

See also toUTC() and toTimeSpec().

qint64 QDateTime::toMSecsSinceEpoch() const

Returns the datetime as milliseconds since the Unix epoch, 1970-01-01T00:00:00 UTC. Returns 0 if the datetime is invalid. The value saturates at the limits of qint64.

See also toSecsSinceEpoch() and setMSecsSinceEpoch().

QDateTime QDateTime::toOffsetFromUtc(int offsetSeconds) const

Returns a copy of this datetime converted to a fixed offset of offsetSeconds from UTC, representing the same instant.

See also toTimeSpec() and setOffsetFromUtc().

qint64 QDateTime::toSecsSinceEpoch() const

Returns the datetime as seconds since the Unix epoch, rounded down. Returns 0 if the datetime is invalid.

See also toMSecsSinceEpoch() and setSecsSinceEpoch().

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

Returns the datetime as text in the given format, or an empty string if the datetime is invalid. ISODate and ISODateWithMs give yyyy-MM-ddTHH:mm:ss, with milliseconds for ISODateWithMs, followed by Z for UTC or the offset such as +10:00. The result is empty for years outside 0 to 9999. RFC2822Date gives dd MMM yyyy hh:mm:ss +hhmm. TextDate gives ddd MMM d HH:mm:ss yyyy, followed by GMT and the offset for UTC and offset datetimes.

See also fromString().

QString QDateTime::toString(const QString &format) const

This is an overloaded function.

Returns the datetime as text using format, or an empty string if the datetime is invalid. It accepts the expressions of QDate and QTime, plus t, which gives UTC or UTC followed by the offset, such as UTC+10:00.

See also fromString().

QDateTime QDateTime::toTimeSpec(Qt::TimeSpec spec) const

Returns a copy of this datetime converted to the time specification spec, representing the same instant. Converting to Qt::OffsetFromUTC keeps the current offset.

See also toLocalTime(), toUTC() and toOffsetFromUtc().

QDateTime QDateTime::toTimeZone(const QTimeZone &toZone) const

Returns a copy of this datetime converted to toZone, representing the same instant. Returns an invalid datetime if toZone is invalid.

This function was introduced in 5.2.

See also setTimeZone() and toTimeSpec().

QDateTime QDateTime::toUTC() const

Returns a copy of this datetime converted to UTC.

See also toLocalTime() and toTimeSpec().