PDA

View Full Version : Regular Expression for QDate [YYYY/MM/DD] format



bera82
21st March 2006, 11:06
Hi,
I have a line edit which takes user input and stores it in a QDATE object in [YYYY/MM/DD] format. I want to construct a regular expression for this. and then use the QValidator to validate the user input against this expression.

It would be helpful if you could give me a regular expression to validate QTIME [HH:MM:SS] for the same purpose.

please help me out.
Regards,
BERA

zlatko
21st March 2006, 11:29
For date try something like that



QRegExp reDate("[0-9]{0,4}/[0-9]{0,2}/[0-9]{0,2}");

wysota
21st March 2006, 11:33
Maybe an input mask (http://doc.trolltech.com/3.3/qlineedit.html#inputMask-prop) would be enough? If not, the easiest regexp validator for yyyy/mm/dd would be [12][0-9][0-9][0-9]/[01][0-9]/[0-3][0-9]. For time: [0-2][0-9]:[0-5][0-9]:[0-5][0-9]. Of course you can make them more complex so that dates like 2006-02-31 are not possible to enter. But it would probably be best to make a QDate based validator - subclass QValidator and reimplement validate() and fixup().

jacek
21st March 2006, 12:49
What about using QDateTimeEdit?

bera82
21st March 2006, 12:55
Hi,

I had used DateTimeEdit but the date format is not the way i need it. I need the date parameters as integers so that i can perform calculations. DateTimeEdit returns date as 11Mar2006! I tried to convert by applying the parameter "Qt::ISODate" but this parameter can be passed only for the "::fromString()" function !!

Thanks,
Regards,
Bera.

jacek
21st March 2006, 13:03
DateTimeEdit returns date as 11Mar2006! I tried to convert by applying the parameter "Qt::ISODate" but this parameter can be passed only for the "::fromString()" function !!
What about QDateTimeEdit::date(), QDateTimeEdit::dateTime() and QDateTimeEdit::time()?

haneen2017
3rd August 2019, 09:40
What about QDateTimeEdit::date(), QDateTimeEdit::dateTime() and QDateTimeEdit::time()?

The code below worked for this format, d/M/yyyy i.e 1/12/2019


QRegExp R_date("([1-9]|[12][0-9]|3[01]{1,2})/([1-9]|[12]{1,2})/(19[0-9][0-9]|20[0-9][0-9])");
QRegExpValidator *valida = new QRegExpValidator(R_date, this);
ui->lineEdit->setValidator(valida);

The code below worked for this format, dd/MM/yyyy i.e 01/12/2019, 02/03/2019


QRegExp R_date("(0[1-9]|[12][0-9]|3[01]{1,2})/(0[1-9]|[12]{1,2})/(19[0-9][0-9]|20[0-9][0-9])");
QRegExpValidator *valida = new QRegExpValidator(R_date, this);
ui->lineEdit->setValidator(valida);