Important: Please read the Qt Code of Conduct -

Regular Expression for date and time(DD/MM/YYYY hh:mm:ss) in QML

  • In QML2 I didn't find any Calender control and I have implemented a control which takes date and time as input and I am using the regular expression for the validation which matches dates including leap year and other validations.

    The main problem is space/backspace should also be considered as a valid for example:

    \s\s/\s\s/\s\s \s\s:\s\s:\s\s

    Following is the code :

        width:parent.width * 0.50
        text : "01/01/2017 00:00:00"
        inputMask: "99/99/9999 99:99:99"
        validator: RegExpValidator { regExp: /^(((([0\s][1-9\s]|[1\s][0-9\s]|[2\s][0-8\s])[\/]([0\s][1-9\s]|[1\s][012\s]))|((29|30|31)[\/]([0\s][13578\s]|[1\s][02\s]))|((29|30)[\/]([0\s][4,6,9]|11)))[\/]([19\s[2-9\s][0-9\s])\d\d|(^29[\/]02[\/]([19\s]|[2-9\s][0-9\s])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)))\s([0-1\s]?[0-9\s]|2[0-3\s]):([0-5\s][0-9\s]):([0-5\s][0-9\s])$/}
        horizontalAlignment: Text.AlignHCenter
        inputMethodHints: Qt.ImhDigitsOnly

    Now, everything works well except for the year and I am not able to match backspace/space for the year and user is not able to clear the year.

    Can you please suggest how to achieve this ? or is there any other method to do this.

  • @pra7 I'm afraid this does not help with your problem, but attempting to deal with validating leap year dates via regular expressions seems extraordinary to me!

  • For your Calendar control:


    But I don't imagine there will be a date+time control. I had to implement that myself in previous (non-Qt) projects.

  • @JNBarchan Thanks for the links. I need calendar control in QML2, not in QML1.

  • @pra7 See also BTW you must mean Quick Controls 2, not QML2. You also can use Controls 1 elements in Controls 2 application, although the look&feel may be a problem.

Log in to reply