- Qt 6.10
- Qt Core
- C++ Classes
- QDateTime
- Qt 6.10.2 Reference Documentation

# Obsolete Members for QDateTime

The following members of class QDateTime are deprecated. They are provided to keep old source code working. We strongly advise against using them in new code.

## Public Functions

`(deprecated in 6.9)` | QDateTime(QDate date, QTime time, Qt::TimeSpec spec, int offsetSeconds = 0)
`(deprecated in 6.9)` void | setOffsetFromUtc(int offsetSeconds)
`(deprecated in 6.9)` void | setTimeSpec(Qt::TimeSpec spec)
`(deprecated in 6.9)` QDateTime | toTimeSpec(Qt::TimeSpec spec) const

## Static Public Members

`(deprecated in 6.9)` QDateTime | fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int offsetSeconds = 0)
`(deprecated in 6.9)` QDateTime | fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec, int offsetSeconds = 0)

## Member Function Documentation

### `[deprecated in 6.9]` QDateTime::QDateTime(QDate date, QTime time, Qt::TimeSpec spec, int offsetSeconds = 0)

This function is deprecated since 6.9. We strongly advise against using it in new code.

Use `QDateTime(date, time)` or `QDateTime(date, time, QTimeZone::fromSecondsAheadOfUtc(offsetSeconds))`.

Constructs a datetime with the given date and time, using the time representation implied by spec and offsetSeconds seconds.

If date is valid and time is not, the time will be set to midnight.

If spec is not Qt::OffsetFromUTC then offsetSeconds will be ignored. If spec is Qt::OffsetFromUTC and offsetSeconds is 0 then the timeSpec() will be set to Qt::UTC, i.e. an offset of 0 seconds.

If spec is Qt::TimeZone then the spec will be set to Qt::LocalTime, i.e. the current system time zone. To create a Qt::TimeZone datetime use the correct constructor.

If date lies outside the range of dates representable by QDateTime, the result is invalid. If spec is Qt::LocalTime and the system's time-zone skipped over the given date and time, the result is invalid.

### `[static, deprecated in 6.9]` QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int offsetSeconds = 0)

This function is deprecated since 6.9. We strongly advise against using it in new code.

Pass a QTimeZone instead, or omit spec and offsetSeconds.

Returns a datetime representing a moment the given number msecs of milliseconds after the start, in UTC, of the year 1970, described as specified by spec and offsetSeconds.

Note that there are possible values for msecs that lie outside the valid range of QDateTime, both negative and positive. The behavior of this function is undefined for those values.

If the spec is not Qt::OffsetFromUTC then the offsetSeconds will be ignored. If the spec is Qt::OffsetFromUTC and the offsetSeconds is 0 then Qt::UTC will be used as the spec, since UTC has zero offset.

If spec is Qt::TimeZone then Qt::LocalTime will be used in its place, equivalent to using the current system time zone (but differently represented).

This is an overloaded function.

See also fromSecsSinceEpoch(), toMSecsSinceEpoch(), and setMSecsSinceEpoch().

### `[static, deprecated in 6.9]` QDateTime QDateTime::fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec, int offsetSeconds = 0)

This function is deprecated since 6.9. We strongly advise against using it in new code.

Pass a QTimeZone instead, or omit spec and offsetSeconds.

Returns a datetime representing a moment the given number secs of seconds after the start, in UTC, of the year 1970, described as specified by spec and offsetSeconds.

Note that there are possible values for secs that lie outside the valid range of QDateTime, both negative and positive. The behavior of this function is undefined for those values.

If the spec is not Qt::OffsetFromUTC then the offsetSeconds will be ignored. If the spec is Qt::OffsetFromUTC and the offsetSeconds is 0 then Qt::UTC will be used as the spec, since UTC has zero offset.

If spec is Qt::TimeZone then Qt::LocalTime will be used in its place, equivalent to using the current system time zone (but differently represented).

This is an overloaded function.

See also fromMSecsSinceEpoch(), toSecsSinceEpoch(), and setSecsSinceEpoch().

### `[deprecated in 6.9]` void QDateTime::setOffsetFromUtc(int offsetSeconds)

This function is deprecated since 6.9. We strongly advise against using it in new code.

Use setTimeZone(QTimeZone::fromSecondsAheadOfUtc(offsetSeconds)) instead

Sets the timeSpec() to Qt::OffsetFromUTC and the offset to offsetSeconds. The datetime may refer to a different point in time.

The maximum and minimum offset is 14 positive or negative hours. If offsetSeconds is larger or smaller than that, then the result is undefined.

If offsetSeconds is 0 then the timeSpec() will be set to Qt::UTC.

See also setTimeZone(), isValid(), offsetFromUtc(), and toOffsetFromUtc().

### `[deprecated in 6.9]` void QDateTime::setTimeSpec(Qt::TimeSpec spec)

This function is deprecated since 6.9. We strongly advise against using it in new code.

Use setTimeZone() instead

Sets the time specification used in this datetime to spec. The datetime may refer to a different point in time.

If spec is Qt::OffsetFromUTC then the timeSpec() will be set to Qt::UTC, i.e. an effective offset of 0.

If spec is Qt::TimeZone then the spec will be set to Qt::LocalTime, i.e. the current system time zone.

Example:

     QDateTime local(QDateTime::currentDateTime());
     qDebug() << "Local time is:" << local;

     QDateTime UTC(local);
     UTC.setTimeSpec(Qt::UTC);
     qDebug() << "UTC time is:" << UTC;

     qDebug() << "There are" << local.secsTo(UTC) << "seconds difference between the datetimes.";

See also setTimeZone(), timeSpec(), toTimeSpec(), setDate(), and setTime().

### `[deprecated in 6.9]` QDateTime QDateTime::toTimeSpec(Qt::TimeSpec spec) const

This function is deprecated since 6.9. We strongly advise against using it in new code.

Use toTimeZone() instead.

Returns a copy of this datetime converted to the given time spec.

The result represents the same moment in time as, and is equal to, this datetime.

If spec is Qt::OffsetFromUTC then it is set to Qt::UTC. To set to a fixed offset from UTC, use toTimeZone() or toOffsetFromUtc().

If spec is Qt::TimeZone then it is set to Qt::LocalTime, i.e. the local Time Zone. To set a specified time-zone, use toTimeZone().

Example:

     QDateTime local(QDateTime::currentDateTime());
     QDateTime UTC(local.toTimeSpec(Qt::UTC));
     qDebug() << "Local time is:" << local;
     qDebug() << "UTC time is:" << UTC;
     qDebug() << "No difference between times:" << local.secsTo(UTC);

See also setTimeSpec(), timeSpec(), and toTimeZone().