Results 1 to 8 of 8

Thread: How to get the custom date and time format???

  1. #1
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default How to get the custom date and time format???

    How to get the custom date and time format???
    2011-07-29 18:55:09

    How to format the date and time to this format??

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get the custom date and time format???

    the class QDateTime provides everything you need.

  3. #3
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get the custom date and time format???

    Qt Code:
    1. QString dateTimeString = dateTime.toString();
    2.  
    3. QDateTime dateTimenew(QDateTime::fromString(dateTimeString, "yyyy-MM-dd hh:mm:ss"));
    To copy to clipboard, switch view to plain text mode 

    I have tried using the above code, but it fails..

  4. #4
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get the custom date and time format???

    what exactly do you want? create a time format to a string? or convert a string to time format?

  5. #5
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get the custom date and time format???

    Could you please explain with my code above??

  6. #6
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get the custom date and time format???

    ok, I'll try.

    you convert a QDateTime (at least I hope it's a QDateTime) into a QString. You use Qt::TextDate as string format. Then you reconvert this string into a QDateTime using a different format ("yyyy-MM-dd hh:mm:ss"). No wonder that fails.

  7. #7
    Join Date
    Jun 2011
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get the custom date and time format???

    try this one:
    Qt Code:
    1. QDateTime dateTime = dateTime.currentDateTime();
    2. QString dateTimeString = dateTime.toString("yyyy-MM-dd hh:mm:ss");
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to get the custom date and time format???

    Quote Originally Posted by Gokulnathvc View Post
    Could you please explain with my code above??
    Can you please, just for once, answer the question put to you. Which do you want; to convert a date/time object to a string, or to convert a string to a date/time object?

    As explained by FelixB your code is doomed to fail because the first line produces a string that the second line does not match. If it did what you seem to intend then this is much faster:
    Qt Code:
    1. QDateTime dateTimenew = dateTime;
    To copy to clipboard, switch view to plain text mode 
    and there is no string involved.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

Similar Threads

  1. Replies: 6
    Last Post: 19th March 2011, 03:39
  2. Replies: 1
    Last Post: 24th April 2010, 15:31
  3. Date format is not display
    By sosanjay in forum Qt Programming
    Replies: 2
    Last Post: 9th November 2009, 10:26
  4. Date and Time format.
    By kaushal_gaurav in forum Qt Programming
    Replies: 3
    Last Post: 12th August 2008, 11:36
  5. Replies: 7
    Last Post: 17th November 2007, 11:33

Tags for this Thread

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.