qttimestamp

How to get the current TimeStamp?


I'm trying to get the current time as TimeStamp without success.

I have this code:

QDateTime setTime = QDateTime::fromString (QString("1970-07-18T14:15:09"), Qt::ISODate);
QDateTime current = QDateTime::currentDateTime();
uint msecs = setTime.time().msecsTo(current.time());

return  QString::number(msecs);

The output is

Sunday, January 25th 1970, 03:17:35 (GMT)

Solution

  • I think you are looking for this function:

    http://doc.qt.io/qt-5/qdatetime.html#toTime_t

    uint QDateTime::toTime_t () const

    Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00, > Coordinated Universal Time (Qt::UTC).

    On systems that do not support time zones, this function will behave as if local time were Qt::UTC.

    See also setTime_t().