Display month and day in localized format

I need to get the correct month and day format for different locales. I have QLable and QDate, the label should display month and day. I am trying QLocale format date.

//Yes, I got system locale and set is as default    
QLocale::toString(date, "MMMM d");

But the result is incorrect.

For example, "MMMM d" in German corresponds to:

"d. MMMM"

for french language:

"d MMMM"

How to convert the "MMMM d" format to local settings in Qt 4.8?

Thank!

PS In javascript I use the following code

var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
var options = {month: 'long', day: 'numeric' };
console.log(date.toLocaleDateString('de-DE', options));
+4
source share
2 answers

So, I used the ICU library. DateTimePatternGenerator solve my problem

0
source

- QLocale::LongFormat:

#include <QtGui>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QList<QLocale> locales;
    locales << QLocale(QLocale::English);
    locales << QLocale(QLocale::German);
    locales << QLocale(QLocale::French);
    locales << QLocale(QLocale::Russian);
    locales << QLocale(QLocale::Chinese);
    locales << QLocale(QLocale::Korean);

    foreach(QLocale locale, locales) {
        QString format = locale.dateFormat(QLocale::LongFormat);

        QRegExp rx("([^d]d(?!d)[^,;]?\\s?|M+.?){2}");
        rx.indexIn(format);
        QString localed = rx.cap(0).trimmed();

        qDebug() << locale.bcp47Name() << "\t" << localed << "\t" << locale.toString(QDateTime::currentDateTime(), localed);
    }

    return app.exec();
}

:

"en-US"          "MMMM d"        "March 17"
"de-DE"          "d. MMMM"       "17. März"
"fr-FR"          "d MMMM"        "17 mars"
"ru-RU"          "d MMMM"        "17 "
"zh-CN"          "M月d日"        "3月17日"
"ko-KR"          "M월 d일"       "3월 17일"
0

All Articles