简体   繁体   中英

How to extract day, month, and year from date returned by QML Calender?

The Calendar clicked signal returns a date as follows:

2015-11-13T00:00:00

However, I would like to have a date formatted like this:

Fri Nov 13 2015

This is what I tried:

onSelectedDateChanged: 
{
     calender.visible = false;
     selectedDate = selectedDate.toLocaleTimeString(Qt.LocalDate, Locale.ShortFormat);
     textOfSelectedDate.text = Date.fromLocaleTimeString(Qt.LocalDate, selectedDate, Locale.ShortFormat)}
}

textOfSelectedDate is the id of the text box where this date will be displayed.

How can I extract day, month, and year in a desired format from Date returned by Calender ?

QML's date type extends Javascript's Date . Thus you can do:

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();
    ...
}

First of all, date is similar to JS date type. So you can use all its functions, like getDate() etc. See it here

Also, you can use Qt.formatDate() object to format the result. In your case it can be as follows:

onClicked: {
    console.log(Qt.formatDate(date,"ddd MMM d yyyy")) 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM