Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with QDateTime
Qt 6.11 is out! See what's new in the release blog

Problem with QDateTime

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mlefloch
    wrote on last edited by
    #1

    Hello everybody,

    I am a beginner in Qt and i wrote my first code to make some tests.
    I used the objects QDate or QTime without problems, but the object QDateTime doesn't work : whatever I do with it, the dateTime of my value is Thu 15. Oct 00:00:00 (even if I use the function currentDate()).

    Have you idees about my problem ?

    Here is my code :

    @QFile file(fileName);
    file.open(QIODevice::ReadOnly);
    QTextStream flux(&file);

    QString line;
    QStringList tempArray;
    QString stringDateTime;
    QDate currDate;
    QTime currTime;
    QDateTime currDateTime;

    while(! flux.atEnd())
    {
    line = flux.readLine();
    tempArray = line.split(',');

    currDate = QDate::fromString(tempArray[0], "dd/MM/yyyy");
    currTime = QTime::fromString(tempArray[1], "hh:mm:ss");

    stringDateTime = tempArray[0] + " " + tempArray[1];
    currDateTime = QDateTime::fromString(stringDateTime, "dd/MM/yyyy hh:mm:ss");
    }
    @

    Thanks !

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      what is your code Output?

      Running this code (very like to yours)

      @
      #include <QCoreApplication>

      #include <QDateTime>
      #include <QStringList>

      #include <QtDebug>

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

      QStringList lines;
      lines << "01/01/2013,10:10:13"
            << "01/12/2013,01:12:55"
            << "01/12/2013,01:13:55"
            << "01/12/2013,01:14:55"
            << "01/12/2013,01:15:55";
      
      QStringList tempArray;
      QString stringDateTime;
      QDate currDate;
      QTime currTime;
      QDateTime currDateTime;
      
      Q_FOREACH (QString line, lines) {
          tempArray = line.split(',');
      
          currDate = QDate::fromString(tempArray[0], "dd/MM/yyyy");
          currTime = QTime::fromString(tempArray[1], "hh:mm:ss");
      
          qDebug() << currDate;
          qDebug() << currTime;
      
          stringDateTime = tempArray[0] + " " + tempArray[1];
          currDateTime = QDateTime::fromString(stringDateTime, "dd/MM/yyyy hh:mm:ss");
          qDebug() << currDateTime;
      }
      
      return 0;
      

      // return a.exec();
      }
      @

      I get this output

      @
      QDate("mar gen 1 2013")
      QTime("10:10:13")
      QDateTime("mar 1. gen 10:10:13 2013")
      QDate("dom dic 1 2013")
      QTime("01:12:55")
      QDateTime("dom 1. dic 01:12:55 2013")
      QDate("dom dic 1 2013")
      QTime("01:13:55")
      QDateTime("dom 1. dic 01:13:55 2013")
      QDate("dom dic 1 2013")
      QTime("01:14:55")
      QDateTime("dom 1. dic 01:14:55 2013")
      QDate("dom dic 1 2013")
      QTime("01:15:55")
      QDateTime("dom 1. dic 01:15:55 2013")
      @

      EDIT: Only to specify that the output string are in Italian Language

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved