qt - 计算两个日期之间的天数

标签 qt qdatetime

我尝试用 Qt 编写一个程序来计算两个日期之间有多少天。问题是我是 Qt 新手,我还没有让它工作。

我想QDateTime很简单,但我不明白程序的结构。

有人可以为我举个例子吗?只是一个简单的程序,例如显示距离圣诞节还有多少天。

最佳答案

你的问题很简单。

在 QtCreator 中创建控制台应用程序,然后按如下方式编辑 main.cpp:

#include <QApplication>
#include <QDate>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // get current date
    QDate dNow(QDate::currentDate());
    // create other date
    //  by giving date 12.21.2012 (joke about end of the world)
    QDate dEndOfTheWorld(2012, 12, 21);
    qDebug() << "Today is" << dNow.toString("dd.MM.yyyy")
             << "Days to end of the world: "
             << dNow.daysTo(dEndOfTheWorld);

    return a.exec();
}

你会得到如下输出:

Today is "18.12.2012" Days to end of the world: 3

P.S.但是我给你的建议是学习C++(添加到你最喜欢的这个主题- The Definitive C++ Book Guide and List),然后学习Qt(我推荐C++ GUI Programming with Qt 4 by Jasmin Blanchette & Mark Summerfield和Summerfields其他书籍)。 祝你好运!

关于qt - 计算两个日期之间的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11187903/

相关文章:

c++ - 将 System::Datetime 转换为 QDateTime

c++ - QDateTime::fromString 返回无效日期,我错过了什么?

c++ - Qt精密定时器计时精度

c++ - 解析 2D QVariantMap

c++ - 使用 Visual Studio/Qt 中止执行

c++ - 列表框对齐问题

c++ - QDateTimeAxis (QtCharts) 中的错误值

c++ - (Qt 或其他)在 Windows 桌面或 explorer.exe 上使用 http 或 ftp 协议(protocol)拖放 mime 类型 uri-list 的数据

python - 如何将 PyQt5 中的 QDate 转换为 datetime.date?