jump to navigation

How to change date formats on Ubuntu January 6, 2009

Posted by ccollins in How To, Linux, Open Source, Ubuntu.
Tags: , , ,
trackback
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.

Comments»

1. Ralph Jones - January 15, 2009

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 - January 15, 2009

@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 - June 7, 2009

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 - October 22, 2009

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 - June 7, 2009

@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 - June 7, 2009

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 - June 7, 2009

@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 - September 11, 2009

@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 - September 12, 2009

@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 - June 10, 2009

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 - August 1, 2009

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 - August 15, 2009

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. ari - September 29, 2009

tks a lot!!!

9. Arne Goetje - October 1, 2009

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.

10. Arne Goetje - October 1, 2009

@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.

11. loki - October 2, 2009

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’).