如何从 QML 日历返回的日期中提取日、月和年?
How to extract day, month, and year from date returned by QML Calender?
Calendar
clicked
信号returns日期如下:
2015-11-13T00:00:00
但是,我希望日期格式如下:
Fri Nov 13 2015
这是我试过的:
onSelectedDateChanged:
{
calender.visible = false;
selectedDate = selectedDate.toLocaleTimeString(Qt.LocalDate, Locale.ShortFormat);
textOfSelectedDate.text = Date.fromLocaleTimeString(Qt.LocalDate, selectedDate, Locale.ShortFormat)}
}
textOfSelectedDate
是将显示此日期的文本框的 id
。
如何从 Calender
返回的 Date
中提取所需格式的日、月和年?
QML 的 date type extends Javascript's Date。因此你可以这样做:
onSelectedDateChanged: {
const day = selectedDate.getDate();
const month = selectedDate.getMonth() + 1; //assuming you want 1..12, getMonth()'s return value is zero-based!
const year = selectedDate.getFullYear();
...
}
首先,date
类似于JS日期类型。所以你可以使用它的所有功能,比如getDate()
等。看到它here
此外,您可以使用 Qt.formatDate()
对象来格式化结果。在您的情况下,它可以如下所示:
onClicked: {
console.log(Qt.formatDate(date,"ddd MMM d yyyy"))
}
Calendar
clicked
信号returns日期如下:
2015-11-13T00:00:00
但是,我希望日期格式如下:
Fri Nov 13 2015
这是我试过的:
onSelectedDateChanged:
{
calender.visible = false;
selectedDate = selectedDate.toLocaleTimeString(Qt.LocalDate, Locale.ShortFormat);
textOfSelectedDate.text = Date.fromLocaleTimeString(Qt.LocalDate, selectedDate, Locale.ShortFormat)}
}
textOfSelectedDate
是将显示此日期的文本框的 id
。
如何从 Calender
返回的 Date
中提取所需格式的日、月和年?
QML 的 date type extends Javascript's Date。因此你可以这样做:
onSelectedDateChanged: {
const day = selectedDate.getDate();
const month = selectedDate.getMonth() + 1; //assuming you want 1..12, getMonth()'s return value is zero-based!
const year = selectedDate.getFullYear();
...
}
首先,date
类似于JS日期类型。所以你可以使用它的所有功能,比如getDate()
等。看到它here
此外,您可以使用 Qt.formatDate()
对象来格式化结果。在您的情况下,它可以如下所示:
onClicked: {
console.log(Qt.formatDate(date,"ddd MMM d yyyy"))
}