chatgpt
1.获取时间戳
#include <QDateTime> #include <QDebug> qint64 timestamp = QDateTime::currentDateTime().toSecsSinceEpoch(); qDebug() << "当前时间戳:" << timestamp; qint64 timestamp = 1615558799; QDateTime dateTime = QDateTime::fromSecsSinceEpoch(timestamp); qDebug() << "时间戳对应的日期时间:" << dateTime.toString("yyyy-MM-dd hh:mm:ss"); QString strDateTime = "2021-03-12 23:59:59"; QDateTime dateTime = QDateTime::fromString(strDateTime, "yyyy-MM-dd hh:mm:ss"); qint64 timestamp = dateTime.toSecsSinceEpoch(); qDebug() << "日期时间对应的时间戳:" << timestamp; QString strDateTime = "2023-03-20 12:34:56"; QDateTime dateTime = QDateTime::fromString(strDateTime, "yyyy-MM-dd hh:mm:ss"); qDebug() << "时间字符串对应的 Qt 时间对象:" << dateTime; QDateTime dateTime = QDateTime::currentDateTime(); QString strDateTime = dateTime.toString("yyyy-MM-dd hh:mm:ss"); qDebug() << "Qt 时间对象对应的时间字符串:" << strDateTime; 计算时间戳时间差 qint64 timestamp = QDateTime::currentDateTime().toSecsSinceEpoch(); qint64 timestamp_60s_ago = timestamp - 60; qint64 time_difference = timestamp - timestamp_60s_ago;