QByteArray Datetime convert
-
Hi,
I have :
QByteArray dateTime = "20230326082227";
it possible in some simple way convert to format:
"2023-03-26 08:22:27"
?
-
Hi,
I have :
QByteArray dateTime = "20230326082227";
it possible in some simple way convert to format:
"2023-03-26 08:22:27"
?
@Damian7546 See if this works for you:
QString dateTime = "20230326082227"; QDateTime date = QDateTime::fromString(dateTime,"yyyyMMddHHmmss"); dateTime = date.toString("yyyy-MM-dd HH:mm:ss"));
This this for more information:
QDateTime::fromString() -
Hi,
I have :
QByteArray dateTime = "20230326082227";
it possible in some simple way convert to format:
"2023-03-26 08:22:27"
?
@Damian7546
Just pick out the character sequences as you want them. The first is 4 characters (2023
), the next is 2 characters (03
), and so on. Use QString::mid() or QString::sliced() for this. Then join the extracted strings back up to your desired output format. -
Hi,
I have :
QByteArray dateTime = "20230326082227";
it possible in some simple way convert to format:
"2023-03-26 08:22:27"
?
@Damian7546 See if this works for you:
QString dateTime = "20230326082227"; QDateTime date = QDateTime::fromString(dateTime,"yyyyMMddHHmmss"); dateTime = date.toString("yyyy-MM-dd HH:mm:ss"));
This this for more information:
QDateTime::fromString() -
@Damian7546 See if this works for you:
QString dateTime = "20230326082227"; QDateTime date = QDateTime::fromString(dateTime,"yyyyMMddHHmmss"); dateTime = date.toString("yyyy-MM-dd HH:mm:ss"));
This this for more information:
QDateTime::fromString() -
@Abderrahmene_Rayene
You could indeed do it this way, I just don't see the point of converting this from a string to a datetime only to put that back into a string. Personal opinion I guess. -
@JonB Because the OP seems to be working with date and time stamps, so I figured they needed a general solution. And it would benefit future readers I hope.
-