QStringList can replace int back?
-
Hi,
I have QStringList that hold time (say 03:13:23 PM). I shall split and change this to 24 hour format and then replace back to same QString.
QString str("03:13:23 PM");
QStringList hrs = qstrData.split(QRegExp("\ "));
int hours = hrs[1].split(":").first().toInt();
if(str.contains("PM") && hours < 12)
{
hours += 12;
}
hrs[1].replace(hrs[1].split(":").first().length(), str.split(":").first().length(), hours);This replaces hrs[1][0] = 0; hrs[1][1] = 12; but expected result is hrs[1][0] = 1; and hrs[1][1] = 2;
Can please any one help to get the desired output.
Thanks in advance. -
Hi,
I have QStringList that hold time (say 03:13:23 PM). I shall split and change this to 24 hour format and then replace back to same QString.
QString str("03:13:23 PM");
QStringList hrs = qstrData.split(QRegExp("\ "));
int hours = hrs[1].split(":").first().toInt();
if(str.contains("PM") && hours < 12)
{
hours += 12;
}
hrs[1].replace(hrs[1].split(":").first().length(), str.split(":").first().length(), hours);This replaces hrs[1][0] = 0; hrs[1][1] = 12; but expected result is hrs[1][0] = 1; and hrs[1][1] = 2;
Can please any one help to get the desired output.
Thanks in advance.@DhanyaRaghav
You should do some debugging, then you'll find hrs[1] is "PM", not "03:13:23".
Yourreplace
statement is too complicated I haven't check that yet.
Why not useQLocale
+QTime
? That's much easier, just one lineQLocale::c().toTime(str, "hh:mm:ss A").toString("HH:mm:ss")
-
you are reinventing the wheel. Use QTime to read the QString time text into the object, then use the class methods to extract or print in the format you wish.
-
you are reinventing the wheel. Use QTime to read the QString time text into the object, then use the class methods to extract or print in the format you wish.
@Kent-Dorfman
Thanks for the reply.
Could you please elaborate much more on it. -
@Kent-Dorfman
Thanks for the reply.
Could you please elaborate much more on it.@DhanyaRaghav
https://doc.qt.io/qt-6/qtime.html#fromString - use it to convert your string into QTime
https://doc.qt.io/qt-6/qtime.html#toString - use it to convert QTime into a QString in format you wish -
@DhanyaRaghav
https://doc.qt.io/qt-6/qtime.html#fromString - use it to convert your string into QTime
https://doc.qt.io/qt-6/qtime.html#toString - use it to convert QTime into a QString in format you wish@jsulm
I have different set of format assigned to qDateTime based on data I receive.QDateTime qDateTime, qDateTimeResult; qDateTime = QDateTime::fromString(qstrData, "MM/dd/yyyy HH:mm:ss AP"); qDateTimeResult = QDateTime::fromString(qDateTime.toString(), "MM/dd/yyyy HH:mm:ss");
DateTimeUtc dateTimeUpdate(qDateTimeResult);
if (qDateTimeResult.isValid())
{
}qDateTimeResult is always invalid, why?
-
@jsulm
I have different set of format assigned to qDateTime based on data I receive.QDateTime qDateTime, qDateTimeResult; qDateTime = QDateTime::fromString(qstrData, "MM/dd/yyyy HH:mm:ss AP"); qDateTimeResult = QDateTime::fromString(qDateTime.toString(), "MM/dd/yyyy HH:mm:ss");
DateTimeUtc dateTimeUpdate(qDateTimeResult);
if (qDateTimeResult.isValid())
{
}qDateTimeResult is always invalid, why?
@DhanyaRaghav said in QStringList can replace int back?:
qDateTimeResult is always invalid, why?
Probably because your qDateTime.toString() returns a different format than "MM/dd/yyyy HH:mm:ss". Why don't you check that? Simply specify the format you need when you call https://doc.qt.io/qt-6/qtime.html#toString
-
@jsulm
I have different set of format assigned to qDateTime based on data I receive.QDateTime qDateTime, qDateTimeResult; qDateTime = QDateTime::fromString(qstrData, "MM/dd/yyyy HH:mm:ss AP"); qDateTimeResult = QDateTime::fromString(qDateTime.toString(), "MM/dd/yyyy HH:mm:ss");
DateTimeUtc dateTimeUpdate(qDateTimeResult);
if (qDateTimeResult.isValid())
{
}qDateTimeResult is always invalid, why?
@DhanyaRaghav You shouldn't use
HH
withAP
, change it tohh
.
Also it will be invalid if the running program's defaultQLocale::amText()
/QLocale::pmText()
is notAM
/PM
.
So in my above code I useQLocale::c()
which is a simplified English locale to make sure that wouldn't happen.
Here you can useQLocale::c().toDateTime(qstrData, "MM/dd/yyyy hh:mm:ss AP")