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. QByteArray Datetime convert
Qt 6.11 is out! See what's new in the release blog

QByteArray Datetime convert

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.3k 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.
  • D Offline
    D Offline
    Damian7546
    wrote on last edited by
    #1

    Hi,

    I have :
    QByteArray dateTime = "20230326082227"; it possible in some simple way convert to format:
    "2023-03-26 08:22:27"

    ?

    JonBJ C 2 Replies Last reply
    0
    • D Damian7546

      Hi,

      I have :
      QByteArray dateTime = "20230326082227"; it possible in some simple way convert to format:
      "2023-03-26 08:22:27"

      ?

      C Offline
      C Offline
      CPPUIX
      wrote on last edited by CPPUIX
      #3

      @Damian7546 See if this works for you:

      QString dateTime = "20230326082227";
      
      QDateTime date = QDateTime::fromString(dateTime,"yyyyMMddHHmmss");
      
      dateTime = date.toString("yyyy-MM-dd HH:mm:ss"));
      

      This this for more information:
      QDateTime::fromString()

      JonBJ 1 Reply Last reply
      1
      • D Damian7546

        Hi,

        I have :
        QByteArray dateTime = "20230326082227"; it possible in some simple way convert to format:
        "2023-03-26 08:22:27"

        ?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @Damian7546
        Just pick out the character sequences as you want them. The first is 4 characters (2023), the next is 2 characters (03), and so on. Use QString::mid() or QString::sliced() for this. Then join the extracted strings back up to your desired output format.

        1 Reply Last reply
        1
        • D Damian7546

          Hi,

          I have :
          QByteArray dateTime = "20230326082227"; it possible in some simple way convert to format:
          "2023-03-26 08:22:27"

          ?

          C Offline
          C Offline
          CPPUIX
          wrote on last edited by CPPUIX
          #3

          @Damian7546 See if this works for you:

          QString dateTime = "20230326082227";
          
          QDateTime date = QDateTime::fromString(dateTime,"yyyyMMddHHmmss");
          
          dateTime = date.toString("yyyy-MM-dd HH:mm:ss"));
          

          This this for more information:
          QDateTime::fromString()

          JonBJ 1 Reply Last reply
          1
          • C CPPUIX

            @Damian7546 See if this works for you:

            QString dateTime = "20230326082227";
            
            QDateTime date = QDateTime::fromString(dateTime,"yyyyMMddHHmmss");
            
            dateTime = date.toString("yyyy-MM-dd HH:mm:ss"));
            

            This this for more information:
            QDateTime::fromString()

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @Abderrahmene_Rayene
            You could indeed do it this way, I just don't see the point of converting this from a string to a datetime only to put that back into a string. Personal opinion I guess.

            C 1 Reply Last reply
            0
            • JonBJ JonB

              @Abderrahmene_Rayene
              You could indeed do it this way, I just don't see the point of converting this from a string to a datetime only to put that back into a string. Personal opinion I guess.

              C Offline
              C Offline
              CPPUIX
              wrote on last edited by
              #5

              @JonB Because the OP seems to be working with date and time stamps, so I figured they needed a general solution. And it would benefit future readers I hope.

              JonBJ 1 Reply Last reply
              0
              • C CPPUIX

                @JonB Because the OP seems to be working with date and time stamps, so I figured they needed a general solution. And it would benefit future readers I hope.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #6

                @Abderrahmene_Rayene
                Absolutely no harm saying so your way :) Preference only. Just make 100% sure the fromString()/toString() work the same way as each other with regard to local time vs universal time.

                1 Reply Last reply
                0
                • D Damian7546 has marked this topic as solved on

                • Login

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