How to change date formats on Ubuntu
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:
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:
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:
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:
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:
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:
All that remains is to log out and log in again, or restart any system services, to see the new format being applied.