Results 1 to 4 of 4

Thread: Date and Time format.

  1. #1
    Join Date
    Jun 2008
    Posts
    89
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Date and Time format.

    Hi,

    I need to display the date and time in a particular format. I have a QDateTime Object.
    How would i use QDateTime::fromString function to get the desired output.

    i have the value of "Tue Aug 12 15:04:11 2008" in my QDateTime object.

    I need to display it as "Today 15:04"

    How would i do it.

    Help!

    Regards
    GK

  2. #2
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Date and Time format.

    I think this is what you are looking for:
    Qt Code:
    1. QString _OldDateTimeString = "Tue Aug 12 15:04:11 2008";
    2. QString _OldFormat = "ddd mmm dd hh:mm:ss yyyy";
    3. QDateTime _DateTime = QDateTime::fromString(_OldDateString, _OldFormat);
    4. QString _NewDateTimeString=_DateTime::toString("ddd");
    5. if(_DateTime.Date()==QDate::currentDate())
    6. {
    7. _NewDateTimeString="Today";
    8. }
    9. NewDateTimeString+=" "+_DateTime::toString("hh:mm");
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2008
    Posts
    89
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Date and Time format.

    Thanks...
    but what if also i want to display "Yesterday" instead of yesterday's date.
    Just as we see in MAC.

    Does date functions support arithmatic (+, -)

  4. #4
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Date and Time format.

    Use daysTo function:
    Qt Code:
    1. QDate _Today=QDate::currentDate();
    2. QDate _SomeDate(2008,8,12);
    3. QString _Day;
    4. qint32 _Difference = _Today.daysTo(_SomeDate)
    5. switch(_Difference)
    6. {
    7. case -1:
    8. _Day="Yesterday";
    9. break;
    10. case 0:
    11. _Day="Today";
    12. break;
    13. case 1:
    14. _Day="Tomorrow";
    15. break;
    16. default:
    17. _Day=_SomeDate.toString("dd mmm");
    18. break
    19. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to stefan for this useful post:

    kaushal_gaurav (12th August 2008)

Similar Threads

  1. time and date issues
    By boog07005 in forum Newbie
    Replies: 5
    Last Post: 20th August 2012, 15:15
  2. automatic time and date updating
    By sudheer in forum Qt Tools
    Replies: 1
    Last Post: 17th January 2008, 10:00
  3. to set date and time in the file info of two days back
    By thomasjoy in forum Qt Programming
    Replies: 1
    Last Post: 11th October 2007, 16:54
  4. chage date and time
    By behnia_k in forum General Programming
    Replies: 4
    Last Post: 18th July 2007, 12:48
  5. QDateTime GMT add sec. or - sec. from locale time....
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 20th February 2007, 16:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.