I need to get correct month and day format for various locales. I have QLable and QDate, label must display month and day. I try format date by QLocale.
//Yes, I got system locale and set is as default
QLocale::toString(date, "MMMM d");
But result is incorrect.
For example, "MMMM d" in German locale equal to:
"d. MMMM"
for French is:
"d MMMM"
How to transform "MMMM d" format to locale settings in Qt 4.8?
Thanks!
P.S. In javascript I use 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));
One way to do it would be to parse 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();
}
Outputs:
"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일"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With