convert a string to QDate in specified format

  • Hi
    i have a date in a string format as below:-

    QString MydateStr;
    QDate MyDate;

    MydateStr = "20200604" //yyyyMMdd

    i want convert this to a QDate and stored this in a SQL server. The SQL server database column is set as below type:-

    CamStartDate DATE NOT NULL,\

    so i can convert the string accordingly to a QDate type using this command:-

    Mydate = QDate::fromString(MydateStr, "yyyyMMdd");

    so after i write the date into the database the format is as below:-


    if i view in debug messages qDebug() <<"Mydate:"<<Mydate;

    the results is this:-

    MyDate: QDate("2020-06-04")

    but now i want the QDate to be in 6/4/2020 format and not as the default which shown as above. so basically i want to convert a string time which is "20200604" into a QDate type but with the format of MM/dd/yyyy. how do i change the QDate format. meaning when i view the debug messages i should get QDate("6/4/2020"), meaning when i write into database it should be stored as 6/4/2020.

  • Lifetime Qt Champion

    I don't see what you want to achieve since 2020-06-04 is exactly the same as 6/4/2020 just in another representation. It doesn't matter how it's shown in a debug string nor how you see it when you take create a select statement in any app - it's the same.
    If you want to show it in this representation somewhere in your app just use QDate::toString().

  • @Christian-Ehrlicher

    so if use QDate::toString() as u said, i need do as below right:-

    QString NewDatestr;

    NewDatestr = MyDate.toString("dd/MM/yyyy");

    but now the NewDatestr is a string and not QDate and the Database column expects a Date type and not string type. As per that i need to have the QDate format to change to dd/MM/yyyy. That what i was asking on how to change the QDate format. The reason is as below:-

    actually on the other HMI which will run the sql query for the database,i can pick two dates and then create an SQL command to read the database, the database is actually populated by the QT program which e.g , will store a row has below:-

    1 209 09:05:19 24-04-2020 16 11 True 02-06-2020
    22 209 09:20:59 2020-05-25 18 14 True 2020-06-02

    the 4th column in the database is a type Date which is default format 24-04-2020 and based on this date i tested that i can query some data as below using the MS SQL query functions:-

    SELECT *
    FROM [dbo].[aztitechCrowdCam209]
    WHERE [CamStartDate] BETWEEN '2020-04-25' AND '2020-05-25'

    so this works i can get the data between the dates i want. As my QT program will insert record as above date format and MS-SQL can query as above SQL statement. But anyway querying from MS-SQL is just a testing to check it working only. I should actually query from another HMI program developed using another software.

    Now my problem is when using another HMI software, in which the software have the 'Dates picking options' and a button to execute the SQL query.

    when i pick the date from this HMI software the date is returned as a string in this format 4/24/2020 and so the SQL statement is constructed as below:

    .......BETWEEN '4/24/2020' AND '5/25/2020'

    and when i query the database it cant convert the dates and returns errors as it expects the dates to be in different format, same as stored by the QT program which is 2020-04-25.

    so i have 2 options now,in the HMI software i can write some coding to re-arrange manually using some string functions so that the returned Date string from the Date picker which is 4/24/2020 becomes 2020-04-24 or i change the format of the Date i storing in the database using the QT program.

  • Lifetime Qt Champion

    @VikramSamy said in convert a string to QDate in specified format:

    WHERE [CamStartDate] BETWEEN '2020-04-25' AND '2020-05-25'

    Here dates are strings, so just use QDate::toString().
    QDate does not have a format, format is something for showing the date and depends on many factors (like locale).

  • @jsulm said in convert a string to QDate in specified format:

    WHERE [CamStartDate] BETWEEN '2020-04-25' AND '2020-05-25'

    yup here are just strings,and it works.... and it works because the database column is a Date type and it can convert the strings to date. but if the date is not arrange as above, then it will have date conversion errors.

    so thats why,when pick 2 dates from the other HMI, the strings format is 6/4/2020 ( MM/dd/yyyy ) and so i need to rearrange it to become 2020-6-4 (yyyy-MM-dd) so that when i query the database, it can convert it to the dates. Anyway i can re arrange the date strings by some string manipulations......
    ....but i was just thinking, the database are written by a QT program, and the date column is Date type and the date is written after converting a received string representing the date using QDate functions. so the QDate function converts the string into this 2020-6-4, so i was just wondering if the Qdate can convert to this format 6/4/2020 rather than to this 2020-6-4, or to any specified format and remain as a Date type and not strings type.

  • Lifetime Qt Champion

    @VikramSamy said in convert a string to QDate in specified format:

    but i was just thinking, the database are written by a QT program

    The database itself is independent from Qt and understands its own data-types - it does not know anything about QDate.
    Of course you can convert QDate to a string of any format, see QDate::toString().

Log in to reply