QML date and time manipulation
-
use Javascript's date
@
var today = new Date() // contains current date and time
@ -
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");
}@
-
Please don't be offending. We all want to be treated respectful.
Thanks
-
hi guys,
i got a problem with time format code,
for example: i would like to display Asian, European timings.
Is their any code, please give me the syntax to display any country timings. -
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. -
hey chris,
Well, i am interested in javascript. i think, for your suggestions need c++.
can please fetch it in javascript, if have any related tutorial, i would be more happy for your concern. I am a newbie to QML.
/sree7k7