Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 830
FlowersLeaves Avatar asked Sep 15 '25 02:09

FlowersLeaves


2 Answers

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();
    ...
}
like image 134
Frank Osterfeld Avatar answered Sep 17 '25 19:09

Frank Osterfeld


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")) 
}
like image 43
folibis Avatar answered Sep 17 '25 20:09

folibis