【问题标题】:QDate::FromString() issue with QDate.year() always returning -4713QDate::FromString() 问题与 QDate.year() 总是返回 -4713
【发布时间】:2014-03-02 15:22:40
【问题描述】:

我正在尝试向 QDate 对象添加日期。月份和日期已正确放入 Qdate 对象中,但年份总是读回 -4713。

这是我的代码:

int main(int argv, char *argc[])
{
   // Show all my arguments to make sure they are correct
   qDebug() << "\n";
   for( int i = 0; i < argv ; i++)
   {
      qDebug() << i << ":  " << argc[i] << "\n";
   }

   // Add the yyyy-mm-dd date into date
   QDate date = QDate::fromString(argc[1],"yyyy-mm-dd");

   // Check to see if the date is correct
   qDebug() << "Year: " << date.year() << "\nMonth:" << date.month() << "\nDay" << date.day();
   return 0;

}

这是我在终端中使用的:

./birthday "1992-01-01"

输出如下:

0 : ./birthdays
1 : 1990-01-01
Year: -4713
Month: 1
Day:   1

现在,无论我为我的年份选择什么,它总是返回 -4713。有谁知道为什么?我已经在这个问题上停留了一段时间,不明白这里发生了什么。

【问题讨论】:

  • 第一个观察:argc 应该是 count 和 argv - 值,这并不重要但仍然))

标签: c++ qt terminal


【解决方案1】:

也许它从 Qt 版本到 Qt 版本有所不同,但对我来说,字符串根本没有转换,直到我写了字母 m - 像 "yyyy-MM-dd" 这样的大写字母然后它开始工作(我的版本是 5.1.0)

你也可以看到它hereMs 提到的是大写。

【讨论】:

  • 是的,月份是 MM。 QTime 和 QDateTime 使用 mm 表示分钟。由于两者都可以在 QDateTime 中混合使用(例如 someDateTime.toString("yyyy-MM-dd hh:mm")),因此区分大小写有点令人困惑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-05
  • 2020-09-02
  • 2019-05-05
  • 2019-05-02
  • 2020-05-03
相关资源
最近更新 更多