QML date and time manipulation
-
Hi,
As Kxyu mentions, you should be able to use the JS Date object (it's internally converted to a QDateTime). For example, you can display a formatted date like this:
@
import Qt 4.7Rectangle {
width: 200
height: 200
Text {
anchors.centerIn: parent
text: Qt.formatDateTime(new Date(), "yyMMdd")
}
}
@Regards,
Michael -
Since I wanted to manipulate the Date() value and not the string I had to construct a javascript function to do just that. QML does not expose all the methods of QDateTime.
@
function getYesterday( ) {
var today = new Date();
var dateTime = new Date( Qt.formatDate(today, "yy/MM/dd") );dateTime.setMinutes( Qt.formatDateTime ( today, "mm" ) ); dateTime.setHours( Qt.formatDateTime ( today, "hh" )-24 ); return Qt.formatDate(dateTime, "yyMMdd");
}
@
The string returned is the value needed.
I would have preferred to do it all in QML the way C++ makes these functions available. -
man, you're insane or something. you function returns crap
I even don't know, if I understood you right, but this returns yesterday's date like yyMMdd@
function getYesterday( ) {
var today = new Date();
today.setDate(today.getDate()-1)
return Qt.formatDate(today, "yyMMdd");
}@
-
Hi,
Take a look at QLocale. You might need to create a helper QML type with a Q_INVOKABLE function with a signature like: QString formattedDateForLocale(const QDateTime &dt, const QString &localeIdentifier).
Cheers,
Chris.