[SOLVED] How to take the system date time?



  • I would like qdebug the datetime but not by running the qdebug 1 time and say qdebug(QDate::currentDate()), but 3 times qdebug.. ( there is a reason )

    i thought about this:
    @QDate date = QDate::currentDate();
    //Mon Aug 27 2012
    QString dateString = date.toString();
    qDebug(dateString.left(3).toLocal8Bit());
    dateString.remove(dateString.left(4));
    qDebug(dateString.left(3).toLocal8Bit());
    dateString.remove(dateString.left(4));
    dateString.remove(dateString.right(5));
    qDebug(dateString.toLocal8Bit());@

    but there must be a better way to do this..

    Plus a 2nd question... if there is better way to this, can it show the day_of_the_week as number and not as letter.. e.g Mon=1, Sun=7..


  • Moderators

    Well, you are converting the date to a string. However, you can use directly QDate which has "dayOfWeek":http://qt-project.org/doc/qt-4.8/qdate.html#dayOfWeek method and probably the others as well.



  • try to give a "format string" argument to method toString()
    like,

    qDebug() << date.toString("ddd");
    qDebug() << date.toString("MMM"); ....

    and for the 2nd question, you can use dayOfWeek() method i think.



  • yes but day_of_week is not a static so i can't use something like:

    @QDate date = QDate::dayOfWeek();
    QString dateString = date.toString();
    qDebug(dateString.toLocal8Bit());@

    what should i put in order to qdebug QDate::dayOfWeek()?



  • QDate date = QDate::currentDate();
    qDebug() << date.dayOfWeek();

    will work.



  • Well yes dayofweek works perfect for me, but i can't find something similar for the month..

    I am really about 30 minutes in this page (http://doc.qt.nokia.com/4.7-snapshot/qdate.html), trying to understand what to do about the month... I know it is just me, but i always founded the Class References too hard to understand, when you haven't seen that specific Class Reference again..

    For example i think what i want is
    QDate::month ()

    In the class reference it says that it is not static, so QDate::month will not work.. what should i do then? there is no further explanation to this.. could anyone explain a bit about the static( and no static) in class references ...? i think then i will figure out what to do with the month thing


  • Moderators

    If a method is static, then it is not tied to a particular instance of that class. However, if a method is not static, then it must have an object of that class to work on.

    For instance:
    @
    // currentDate() is static. It's used with the class name, QDate::
    QDate mydate = QDate::currentDate();

    // isValid() is not static. It requires an instance of an object to work upon. (mydate in this case.)
    bool valid = mydate.isValid();
    @

    bq. what should i do then?

    Create an instance of QDate, then you can use any of the QDate methods on that object.


  • Moderators

    [quote author="Leon" date="1346098398"]Well yes dayofweek works perfect for me, but i can't find something similar for the month..

    I am really about 30 minutes in this page (http://doc.qt.nokia.com/4.7-snapshot/qdate.html), trying to understand what to do about the month... I know it is just me, but i always founded the Class References too hard to understand, when you haven't seen that specific Class Reference again..

    For example i think what i want is
    QDate::month ()

    In the class reference it says that it is not static, so QDate::month will not work.. what should i do then? there is no further explanation to this.. could anyone explain a bit about the static( and no static) in class references ...? i think then i will figure out what to do with the month thing
    [/quote]

    There is not more explanation, because there is not much more to say.

    I think you need to describe a bit in more detail, what you are trying to achieve. Otherwise you will get a large number of responses, but not the solution you are looking for.

    Apparently, you need some of the information of the current date as integer. All methods are available in "QDate":http://qt-project.org/doc/qt-4.8/qdate.html Yes, they are non-static, but it would no sense to have them as static. QDate is simply an object handling dates. It may be today, tomorrow, end of the year, or whatever you like. The static method for currentDate is more a convenience method, because a lot of applications like to know the current date.

    You started out already with getting the date, so you can complete like this:
    @QDate date = QDate::currentDate();
    //Mon Aug 27 2012
    // QString dateString = date.toString();
    // qDebug(dateString.left(3).toLocal8Bit());
    // dateString.remove(dateString.left(4));
    // qDebug(dateString.left(3).toLocal8Bit());
    // dateString.remove(dateString.left(4));
    // dateString.remove(dateString.right(5));
    // qDebug(dateString.toLocal8Bit());
    int dow = date.dayOfWeek();
    int doy = date.dayOfYear();
    int month = date.month();
    @
    Again, you might to tell us a bit more about your intentions.



  • Yes the information was helpful and it solved my problem. Thank you guys!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.