PDA

View Full Version : qdate from string with one or two day digits and one or two month digits



TorAn
14th January 2018, 00:34
Can't figure out how to specify format for QDate::fromString call where the string is a date with one or two digits for day and month. For example: 1/12/2018 or 01/12/2018, or 1/1/2018 vs 01/01/2018. Advice is greatly appreciated.

Thanks

Szyk
14th January 2018, 14:44
I am afraid that you have to call 4 times QDate::fromString (with different params for each case) and verify it by QDate::isValid...

d_stranz
14th January 2018, 18:48
So don't use QDate::fromString().



QStringList dateList = dateString.split( '/' );

QDate date;
if ( dateList.length() == 3 )
{
int month = dateList.at( 0 ).toInt();
int day = dateList.at( 1 ).toInt();
int year = dateList.at( 2 ).toInt();
date.setDate( year, month, day );
}
else
// error