Coding

How to change date formats on Ubuntu


[tweetmeme source=”gosub3000”]

The Date Stamp

The Date Stamp

In order to change the system date format on Ubuntu, you need to know a little about where the date format is coming from.  Regional settings, such as date and time formats, as well as language, sort order, etc. are specified in files called locales.  A locale contains the rules specifying how dates and times are formatted, amongst other settings.

To find which locale you are currently running, you can issue the following command in a terminal:

locale

On my system this returns:

LANG=en_IE.UTF-8
LC_CTYPE="en_IE.UTF-8"
LC_NUMERIC="en_IE.UTF-8"
LC_TIME=en_IE.UTF-8
LC_COLLATE="en_IE.UTF-8"
LC_MONETARY="en_IE.UTF-8"
LC_MESSAGES="en_IE.UTF-8"
LC_PAPER="en_IE.UTF-8"
LC_NAME="en_IE.UTF-8"
LC_ADDRESS="en_IE.UTF-8"
LC_TELEPHONE="en_IE.UTF-8"
LC_MEASUREMENT="en_IE.UTF-8"
LC_IDENTIFICATION="en_IE.UTF-8"
LC_ALL=

Here I can see that my date and time settings are being specified by “LC_TIME=en_IE.UTF-8”.  LC_* are environment variables you can use to modify particular uses of your locales.  Run “man locale” for more information.  The “en_IE” code tells me that I’m using English (en) for the Ireland (IE) region, thus I have a default date format of “06/01/09”, or “%d/%m/%y” format.  Date formats here are compatible with the “date” command.

Say for example, I would prefer to use the German date format of “06.01.2009”, or “%d.%m.%Y”.  How would I achieve this?

A simple answer would be to configure your system to use a German locale, by editing /etc/environment and adding (or modifying) the line:

LC_TIME=de_DE.UTF-8

The problem with this approach, is that more than dates and times are now changed, such as day and month names (now being in German).

A better way would be to customise your current locale (en_IE in my case).  To do this, change directory to /usr/share/i18n/locales.  Here you will find many locales for many regions.  Choose the locale you wish to customise and copy it by executing:

sudo cp en_IE custom

Next chose the date or time format string you would like.  In our case it will be “%d.%m.%Y”.  You can check and modify this string using the date command, as in:

date +%d.%m.%Y

If this returns the date in the format you would like, then you know you have the right format string.  You can find all format codes if you use “man date”.

The date format string, is specified in the locale file using a Unicode notation.  Open our custom locale using your favourite text editor:

sudo gedit custom

The date format is specified on the line beginning “d_fmt”, and looks like:

d_fmt  "<U0025><U0064><U002F><U0025><U006D><U002F><U0025><U0079>"

You will now have to convert your date format string to Unicode.  You can do this, by looking up the Unicode equivalent for each character on http://asciitable.com/.  In this way “%” becomes “<U0025>”, “d” becomes “<U0064>”, “.” becomes “<U002E>”, and so on.  Replace the d_fmt line with your new format string:

d_fmt   "<U0025><U0064><U002E><U0025><U006D><U002E><U0025><U0059>"

The same process can be used to modify the datetime format (d_t_fmt), date format (d_fmt), time format (t_fmt), am and pm format (am_pm), and standard 12 hour notation (t_fmt_ampm), as well as other locale settings.

Save and exit your text editor.  You now have a custom locale in the file “custom”.  In order for the system to use it, you need to compile it into a system readable locale definition.  This can be done using the locale compiler by executing:

sudo localedef -f UTF-8 -i custom custom.UTF-8

Now the new custom locale is available to the system, you need to configure the system to use it.  Do this by editing the file /etc/environment (sudo gedit /etc/environment) and adding (or modifying) the line:

LC_TIME="custom.UTF-8"

All that remains is to log out and log in again, or restart any system services, to see the new format being applied.

Standard

41 thoughts on “How to change date formats on Ubuntu

  1. Ralph Jones says:

    Does this work for X locales as well? Last I heard is that it doesn’t.
    Another workaround is to use the pre-built en_DK locale.

    eg. LC_TIME=”en_DK.UTF-8″

  2. ccollins says:

    @Ralph,
    As far as I’m aware (and I could be wrong on this), this should work for all applications and services which use standard system locale calls.

    -Chris

  3. Nino says:

    Hey guys!
    Setting LC_Time = “en_DK.UTF-8″ doesn’t solve anything. I still have to type month and day in reverse order. More annoying, if I set LC_time=”da_DK.UTF-8” it is still reverse order, just with danish names.
    The steps with writing hexcodes are somewhat complicated. Unbelieveable it takes so much hassle just to change the time format!

    • Benoit says:

      How about LC_TIME=”en_GB.UTF-8″ if the GB order is day/month/year.

      But where do I go to set it up?

      Not in anyone’s .bashrc nor .profile, if I want it for general purpose.

  4. ccollins says:

    @Nino,
    I agree. I was surprised at the amount of effort required just to change the date format.

    I guess this represents a good opportunity for an aspiring software engineer to create a “Regional Settings” type applet for Linux. It wouldn’t be that difficult, and would increase the usability of Linux for this type of operation.

    -Chris

    • Nino says:

      I look forward to this day. Unfortunately after following you instructions the GUI for Language Settings crashes everytime. So now I don’t know how to reverse the changes. Cannot find the UTF-8 file i created in the system

      • ccollins says:

        @Nino
        Oh dear. I hope you haven’t lost any data.

        To undo any changes, reedit /etc/environment to its original settings. Then you can find the newest files in /usr/share/i18n/locales by going there and doing an “ls -lrt”

        Hope this helps.
        -Chris

      • Lonemass Duke says:

        @ccollins
        The crash wouldn’t go…
        i have spent some time now…
        the gnome-language-selector tool crashes giving the following:

        /usr/lib/python2.6/dist-packages/LanguageSelector/gtk/GtkLanguageSelector.py:839: GtkWarning: gtk_cell_view_set_cell_data: assertion `cell_view->priv->displayed_row != NULL’ failed
        cell = combo.get_child().get_cell_renderers()[0]
        Traceback (most recent call last):
        File “/usr/bin/gnome-language-selector”, line 34, in
        options=options)
        File “/usr/lib/python2.6/dist-packages/LanguageSelector/gtk/GtkLanguageSelector.py”, line 171, in __init__
        self.updateUserDefaultCombo()
        File “/usr/lib/python2.6/dist-packages/LanguageSelector/gtk/GtkLanguageSelector.py”, line 58, in wrapper
        res = f(*args, **kwargs)
        File “/usr/lib/python2.6/dist-packages/LanguageSelector/gtk/GtkLanguageSelector.py”, line 858, in updateUserDefaultCombo
        COMBO_LANGUAGE,self._localeinfo.translate(locale),
        File “/usr/lib/python2.6/dist-packages/LanguageSelector/LocaleInfo.py”, line 148, in translate
        return self.translate_language(locale)
        File “/usr/lib/python2.6/dist-packages/LanguageSelector/LocaleInfo.py”, line 110, in translate_language
        lang_name = gettext.dgettext(‘iso_639’, self._lang[lang])
        KeyError: ‘custom’

        when i edited the python code i found out that custom is still there.. but it cannot load it…

      • Lonemass Duke says:

        @Nino, ccollins…

        I was able to solve it after all…
        all i had to do is :
        sudo locale-gen –purge

        before doing that, you can check that
        locale -a
        gives a list of locales with custom included…
        after regenerating with the option –purge…
        “custom goes to hell”

        cheers…

  5. Mauro says:

    Hi, I’m trying to do the same with my Ubuntu 8.04, but doesn’t seem to work.

    I’d like to change the date format from month-day-year to day-month-year systemwide… so what I did is simply add the line
    LC_TIME=”it_IT” (italy)
    to the /etc/environment file and restarted the system but it doesn’t seem to work… do you have any suggestions? (is there an english country that supports this date layout?)

    Thanks!!!

  6. Peter says:

    Yes Mauro, In the UK and Ireland the standard format is day-month-year, believe it or not. Only the USA uses month-day-year. Why? Shoot me! It’s the most ridiculous way of writing dates. I personally want to have ISO for all, that is year-month-day as that is the most logical. Put the most significant piece of information first and time to be written in a 24 hour format. This message board does not support this either, but of course being USA breed I’m not surprised…
    If someone could write something for Linux to simply choose the format, like in Uncle Bill’s OS, that would be nice.

  7. kaddi says:

    KDE always had options for regional settings, you can simply set the way you want your date/time/money/numbers displayed…
    This is a pure Gnome problem.

    So just switch to Kubuntu and it can simply be edited with Systemsettings. ;)

  8. Arne Goetje says:

    gnome-language-selector crashes, because ‘custom’ is *not* a valid locale code! Please use something like ‘en_IE.UTF-8@custom’. Nevertheless, it won’t show up in the list of available languages, since the variant name (@custom) is not ‘translatable’ yet. Language-Selector gets the language and country names from the ISO639 and ISO3166 standards respectively and translates them into the native language by using the translations of the iso-codes package. The entry then shows up as ‘Language (Country/Region)’ translated into the language given by the language code. Since the @variants are not registered anywhere, we currently cannot translate them. Therefor they are currently ignored. (Also Launchpad cannot handle @variations yet, so it’s not even possible to work on the translations online.) We are planning to fix this, maybe we can do it in the Lucid cycle.

  9. Arne Goetje says:

    @kaddi:
    yes, KDE stores locale related settings internally somewhere and does not use the system locales. If you customize the settings however, only KDE software will be able to use it.
    And yes, it would be nice if Gnome would have that feature too, but it would be even nicer, if there would be a common standard where and how to store such customizations, so that *any* software can use them, no matter on which desktop it runs on.

  10. loki says:

    trying to change my thunderbird/lightning calendar to a 24h format/representation such as 15:30 instead of 3:30 pm
    by following the instructions to change the system format (which a friend told me to do) but i don’t know which of the settings (t_fmt, am_pm, t_fmt_ampm) is the right one and where i could find the right options (i haven’t found anything suitable in ‘man date’).

  11. Indie says:

    cd /usr/share/i18n/locales
    localedef -ci en_GB -f UTF-8 /usr/lib/locale/en_GB.utf8

    and it will update the files used for the locale and not the localarchive

  12. Janis says:

    You may try to explain it to your mother.
    Who invented this BUG and linked varios very different things to language?

  13. Kevin O. Grover says:

    I haven’t played with this yet. However, before attempting to manually convert strings to the locale format, I created a little script: str2locale:

    #!/usr/bin/env python
    import sys
    for arg in sys.argv[1:]:
    arg = unicode(arg)
    print ”.join([“” % ord(c) for c in arg])

    $ # ISO Date
    $ ./str2locale “%Y-%m-%d”

    $ # ISO Time
    $ ./str2locale “%H:%M:%S”

    It’s too bad there just isn’t an ISO locale that lets use have strings in our language (English for me) but dates and times in standard format: I’ve been using the ISO formats for a _long_ time now.

  14. jza says:

    Simply adapt the “d_fmt” in the /usr/share/i18n/locales/my-lc_time-file-name was perfect in my system (debian 6 squeeze), as it is explained.
    But I just commented the original “d_fmt” string and write my one (d-m-Y) :
    “”
    Finished with linux command : locale-gen , then logout/login and it’s done !
    Thanks

  15. don3 says:

    Seems to me the locale system is more or less the right approach, as it does not depend on any particular graphical environment – so character sessions and daemons can benefit too – but it’s lacking in areas like end-user configurability, as has been mentioned already. KDE (and GNOME, etc) could have built on top this rather than “rolling their own”, and hopefully that might have resulted in fixing the current deficiencies.

    Like other commenters, I would like to start with the default settings for my language (en_US), but override some of them, such as date/time formats (ISO with no ‘T’, 24-hr), starting day of week (Mon), monetary format, measurement units, … Creating a custom locale definition would be OK for machines for which I have root permission, but from what I’ve seen in the man pages, there’s no way to set up an alternate locale database. By way of comparison, the terminfo system can be pointed to any installation directory simply by setting an environment variable (TERMINFO), which allows me to override broken or missing terminal definitions on some systems I use but don’t own. It would be great if some variable like LOCALEDB or LOCALEPATH could be used to replace or augment the system-wide installation that the locale library code uses.

    • don3 says:

      …and while I’m dreaming up features without providing patches, I think it would also be very nice to be able to override individual definitions (“keywords”) through the environment, without having to set up a definition file at all. For example

      LC_TIME.d_t_fmt=’%Y-%m-%d %T %Z’ ls -l

      would have the same effect as

      TIME_STYLE=’%Y-%m-%dT%T %Z’ ls -l

      but would work on any locale-aware program. (And yes, I’m assuming it would accept at least 7-bit ASCII characters as-is, without needing conversion to Unicode syntax.)

      Then I suppose graphical environments would have a choice of maintaining local locale databases, or just setting overrides in their environments.

  16. Pingback: How do I set the representation of dates? Drija

  17. Pingback: How do I set the date format to ISO globally in Linux? - Admins Goodies

    • ccollins says:

      Hi Neo,
      That’s fine for gnome applications, but won’t work for system services.

      -Chris

    • ccollins says:

      @anandb
      It’s complicated.

      The post here describes how to change the system local setting, which is the basis for all system related date formatting. Changing this value will affect how dates and times are processed by the operating system.

      That said, some projects administer their date formats separately from the system locale, such as LibreOffice. You should check the documentation for each application to find out for sure.

      -Chris

  18. Pingback: Date format in the files written by syslogd - The UNIX and Linux Forums

  19. Choperro says:

    This is one of the good examples where you can clearly see that Ubuntu is not end-user oriented. It is not for human beings.

    • ccollins says:

      @choporro,
      I wouldn’t go that far. While I based my example on Ubuntu, all Linux variants have similar issues. It’s not fair to blame Ubuntu only.

    • ccollins says:

      Thanks Sergio. Now that you point it out, it seems obvious that you should be able to do that!

      -Chris

  20. Mike says:

    Thanks alot! This was very helpful :D
    I had managed to edit but couldn’t work out the localedef part until I read you post.

  21. Pingback: How do I set the date format to ISO globally in Linux? - Just just easy answers

  22. Pingback: 設定無宿:Restoring Ubuntu 12.04 64bit environment to Folio 13 with ubuntu clean install | Muttley's Web Monolog

  23. Gerry says:

    I’ve set locale to en_DK on ubuntu 14.04. Yet Nautilus still displays non-sensical United States date-time format. How do I get Nautilus to honor the locale setting?

  24. A says:

    Ubuntu 14.04 doesn’t have a LC_TIME= line in /etc/environment. Can you tell how to get ubuntu to use the custom? Thanks

Comments are closed.