3

The problem

In QGIS 3.34 Prizren, I have an attribute field of date type. Unfortunately, years are shown only with two digits: 23 instead of 2023. As a historian, this is really bad as it could be also 1923, 1823 etc. I know that internally, QGIS uses the correct, 4-digit year values. But for reading the attribute table, I want to see the full numbers.

The Question

How can I show the date in a format with years with actual, four-digit numbers?

What I tried

I tried to use format_date("date", 'dd. MM. yyyy'), however, to no avail: in the preview, the desired format shows up, but when the field is created, all values are set to NULL. This applied to temporary scratch layers as well as to GeoPackage layers.

I also tried Menu Settings > Options > General. I can change the Locale settings, but this does not affect the two-digit representation of the years.


The issue seems to be a newer one, as a year before (must have been QGIS 3.28), with the same settings, I had 4-digit numbers for the year (compare with 2nd screenshot below). Setting the locale to "French France" works (after restart of QGIS, using hint of @user30184). However, I would like to get 4-digit years in my locale "German Switzerland" as it was possible in older versions.

It is also strange that in my Windows 10 system Region settings, the year appears in 4 digits, even the "short version" (see screenshot, highlighted in red). It seems that in newer versions, QGIS does not respect system settings...

enter image description here

A workaround

I could, of course, create a new field of type text/string and create a correctly looking date, but I try to avoid this as it is not very elegant and error-prone.

enter image description here

enter image description here

14
  • 1
    For me the Locale setting does have an effect but maybe you do not want to use for example "French France" locale. I do not know how to override the format for a selected locale.
    – user30184
    Nov 19, 2023 at 17:49
  • 4
    format_date returns a string representation of a date, which means that you cannot enter it into a field of type "Date". That's why those are all NULL.
    – Andre Geo
    Nov 19, 2023 at 18:57
  • 1
    What operating system are you using? If Windows, check the Short Date settings in your region/locale. Nov 19, 2023 at 23:25
  • 1
    Hi Babel, I can reproduce the error. Since 3.34, it shows the timeformat in the official locale (localeplanet.com/icu/gsw-CH/index.html). If you use the C default (C), it uses the system default, but the long format. Perhaps there is a possibility you can override the format? Nov 20, 2023 at 9:35
  • 1
    I meant the Attributes Form, rather than the Fields. I can change the Date format - see: i.stack.imgur.com/t1Xrz.png Nov 20, 2023 at 22:35

2 Answers 2

5

Under Layer Properties -> Attributes Form

enter image description here

You can select the field and control the Widget that it uses to display that field. In the case of Date fields, this includes controlling the format.

enter image description here

I'm not entirely sure why the default format in QGIS doesn't match the default system format, but at least there appears to be an avenue to control it.

5

Explanation:

First, let me clarify the meaning of the code in the brackets (de_CH):

This code refers to Unicode (ICU) Locale Formats, a database that stores local information such as date format, currency, and language. QGIS accesses this locale information through the QLocale class (see https://doc.qt.io/qt-5.15/qlocale.html). Changing anything in your system settings will not affect the representation of the date format because you are overriding the default system settings.

It's possible that the development team, either from Qt or QGIS, has made changes resulting in the application now accessing the short datetime format from the de_CH locale instead of the medium format (as described in more detail at https://leap.hcldoc.com/help/topic/SSS28S_8.2.1/XFDL_Specification/i_xfdl_r_formats_de_CH.html).

Approach

enter image description here By choosing C default (C), you opt for the default settings commonly used within programming environments on Windows. In @Babel's case, this corresponds to his requested 4 digit time format.

Another approach is stated in @Tom Brennan answer, but this cannot be set globally over all attribute tables. However, it's worth noting that Tom's answer is also valid and makes sense in certain applications.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.