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. Universal way to parse QString with non-fixed date-time formats for QDateTime conversion
Forum Updated to NodeBB v4.3 + New Features

Universal way to parse QString with non-fixed date-time formats for QDateTime conversion

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 398 Views 2 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.
  • Q Offline
    Q Offline
    Qnut
    wrote on 29 Jul 2021, 14:41 last edited by
    #1

    Hi

    I need to parse a QString and convert it to QDateTime. My Problem is that the string is in users locale format, e.g. "24-4-2021 16:17:18" or "4/24/2021 16:17:18" or "24.04.2021 16:17:18". Depending on users locale even text format is possible like "Apr 24, 2021 4:17:18 pm".

    Microsofts MFC has a function COleDateTime::ParseDateTime() that was able to handle different formats. Is their something similar in Qt or does anybody know about an open source solution?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 29 Jul 2021, 18:00 last edited by
      #2

      Hi,

      Isn't QLocale::toDateTime what you are looking for ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • Q Offline
        Q Offline
        Qnut
        wrote on 30 Jul 2021, 13:24 last edited by
        #3

        Thanks for the answer.

        I am afraid QLocale::toDateTime still depends of the current QLocale setting. e.g. a QLocale("de_DE") cannot parse "3:45:21 pm" as german time has no concept of am/pm.

        May be I can check several locales and can cover the most frequent time presentations used by the customers. I'll take a look at it ...

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 30 Jul 2021, 19:08 last edited by
          #4

          @Qnut said in Universal way to parse QString with non-fixed date-time formats for QDateTime conversion:

          COleDateTime::ParseDateTime

          Checking the documentation of that method, it does not seem to be as universal as you describe it. It also uses the system locale by default.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          1/4

          29 Jul 2021, 14:41

          • Login

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