在Qt中,可以使用QDateTime类来获取年月日时间,并将其存储在int型变量中。以下是一个示例代码:

#include <QDateTime>

int main()
{
    // 获取当前日期和时间
    QDateTime currentDateTime = QDateTime::currentDateTime();

    // 获取年份
    int year = currentDateTime.date().year();

    // 获取月份
    int month = currentDateTime.date().month();

    // 获取日期
    int day = currentDateTime.date().day();

    // 输出年月日时间
    qDebug() << 'Year: ' << year;
    qDebug() << 'Month: ' << month;
    qDebug() << 'Day: ' << day;

    return 0;
}

在上面的代码中,首先使用QDateTime::currentDateTime()获取当前日期和时间。然后,使用date()方法获取日期部分,再使用year()month()day()方法分别获取年、月和日,并将它们存储在int型变量中。最后,使用qDebug()输出结果。

请注意,要在Qt项目中使用QDateTime类,需要包含<QDateTime>头文件。

Qt 获取年月日时间并存储到int型变量

原文地址: https://www.cveoy.top/t/topic/bf5t 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录