PDA

View Full Version : QLocale and date



jiveaxe
28th April 2012, 18:38
Hi, have a problem with localized date representation. Dates are in the following format:

2012-05-01

My system is using the it_IT locale as QLocale::system().name() confirms. But


date.toString(Qt::SystemLocaleShortDate)

returns the english string "1 May 2012" instead of the italian "1 Mag 2012"

Seems that the only option to have an italian localization is to use:


QLocale locale;
QLocale::setDefault(locale.name());
locale.toString(date, QLocale::ShortFormat)

But the returned string is "01/05/2012", while if using QLocale::LongFormat I got "martedì 1 maggio 2012" that is correct, but too long.

With locale.toString(date, "dd MMMM yyyy") I obtain the desired format but since I will release the code I would prefer a more clean solution.

Thanks.

JonnyJP
21st May 2012, 08:45
I'm having a similar problem too.

We need to localise a date to display someone's date of birth so are using QLocale::dateFormat. However, this only returns the short format ('11/06/2006') or the long format ('Domingo, 11 de Junho de 2006') and we want an equivalent to the long format without the day name.

If we strip out 'dddd' from the date format we end up with ', 11 de Junho de 2006' which is wrong as there is a comma and no guarantee it is grammatically correct in the given language. For example, some languages may say 'the saturday' instead of 'saturday' so stripping out the day name will leave in an extra 'the'.

Obviously, I can pass in my own date format as jiveaxe does above but then that isn't localised properly.

AlekseyOk
21st May 2012, 09:37
Hi! You can parse string returned from QLocale::dateFormat() to remove unnecessary date parts...

QString::split(" ")