Ubuntu server 20.04 - time format 24 hours on shell with date command
The easiest way is to change LC_TIME
variable to locale that uses desired time formatting. en_GB
uses 24h clock for example. You can set it system-wide using localectl
:
localectl set-locale LC_TIME="en_GB.UTF-8"
Then, you have to relogin to see changes.
Alternatively, you can customize date and time formatting in locale definition file /usr/share/i18n/locales/en_US
. You will find date and time formatting in the following section:
% Appropriate date and time representation (%c)
d_t_fmt "%a %d %b %Y %r %Z"
%
% Appropriate date representation (%x)
d_fmt "%m//%d//%Y"
%
% Appropriate time representation (%X)
t_fmt "%r"
%
% Appropriate AM/PM time representation (%r)
t_fmt_ampm "%I:%M:%S %p"
%
% Appropriate date and time representation for date(1)
date_fmt "%a %d %b %Y %r %Z"
In this case, to get 24h clock, you have to replace %r
to %T
and run locale-gen
to regenerate locales.
Related videos on Youtube
MadHorse
Updated on September 18, 2022Comments
-
MadHorse over 1 year
I've installed Ubuntu server 20.04, and I can't find a way to display the time format to 24 hours when I use the command 'date' in the shell. I check the locale information with an Ubuntu server 18.04 and all seems the same, but on 18.04 i have the date in 24 hours format
Mon May 11 22:41:08 CEST 2020
while in 20.04 is "different" and in 12 hours format
Mon 11 May 2020 10:41:40 PM CEST
The following are the result of the locale command Ubuntu 18.04
LANG=en_US.UTF-8 LANGUAGE= LC_CTYPE="en_US.UTF-8" LC_NUMERIC="en_US.UTF-8" LC_TIME="en_US.UTF-8" LC_COLLATE="en_US.UTF-8" LC_MONETARY="en_US.UTF-8" LC_MESSAGES="en_US.UTF-8" LC_PAPER="en_US.UTF-8" LC_NAME="en_US.UTF-8" LC_ADDRESS="en_US.UTF-8" LC_TELEPHONE="en_US.UTF-8" LC_MEASUREMENT="en_US.UTF-8" LC_IDENTIFICATION="en_US.UTF-8" LC_ALL=
Ubuntu 20.04
LANG=en_US.UTF-8 LANGUAGE= LC_CTYPE="en_US.UTF-8" LC_NUMERIC="en_US.UTF-8" LC_TIME="en_US.UTF-8" LC_COLLATE="en_US.UTF-8" LC_MONETARY="en_US.UTF-8" LC_MESSAGES="en_US.UTF-8" LC_PAPER="en_US.UTF-8" LC_NAME="en_US.UTF-8" LC_ADDRESS="en_US.UTF-8" LC_TELEPHONE="en_US.UTF-8" LC_MEASUREMENT="en_US.UTF-8" LC_IDENTIFICATION="en_US.UTF-8" LC_ALL=
This is the result of command localectl
Ubuntu 18.04
System Locale: LANG=en_US.UTF-8 VC Keymap: n/a X11 Layout: it X11 Model: pc105
Ubuntu 20.04
System Locale: LANG=en_US.UTF-8 VC Keymap: n/a X11 Layout: it X11 Model: pc105
I don't know where to look at to change the time in 24 Hours format like in Ubuntu 18.04. Can someone give me some help ?
Thank you
-
MadHorse almost 4 yearsHi Mateusz, thank you for your answer. I used the command
localectl set-locale LC_TIME=it_IT.UTF-8
and now the date is correct, but it changed all theLC_*
settings to"C.UTF-8"
I don't know if this may cause some problems. The strange thing is that i didn't had to change anything on the previous Ubuntu versions to have the date displayed in 24-hours format. -
Mateusz almost 4 yearsHi,
C
is default locale when no locale is set, and it should not cause any problems. I suppose that now you seeC
value because you do not have any locale set to mainLANG
variable. To check your current configuration see file/etc/default/locale
. If you want, you can setLANG
by runninglocalectl set-locale en_US.UTF-8
or by manually editing config file. -
Mateusz almost 4 years24-hour clock in en_US locale was a bug in glibc locale definition and has been corrected in this commit. So, the change was intentional.
-
MadHorse almost 4 yearsThank you, now it make sense. Tomorrow when i get back to work i will try to set
LANG
, but I would like to understand correctly what I'm doing. TheLANG
variable at the beginning was set toen_US.UTF-8
, with the commandlocalectl set-locale LC_TIME=it_IT.UTF-8
only the variableLC_TIME
had to change, but all the variablesLC_TIME
included, changed toC.UTF-8
. Shouldn't just change only theLC_TIME
variable and leave the others variables unchanged ? -
Mateusz almost 4 yearsYour understanding is correct.
localectl
should have just add definition forLC_TIME=it_IT.UTF-8
in/etc/default/locale
. I tested this before I posted my answer, and it worked as expected. It looks like you have lost somehow your originalLANG
value, but unfortunately I do not know why. This would explain that allLC_*
changed its value. Normally allLC_*
variables are not explicitly defined. You define one of them when you want to override some locale setting. When someLC_
variable is not defined thelocale
command will show it with value inherited fromLANG
. -
MadHorse almost 4 yearsOk, I found the problem, with the command
locale -a
I have a list of all the avialable locale and there wasn't theit_IT.utf8
so i use the commandlocale-get it_IT.utf8
to generate the locale and then rebooted. Now I have all the locale setting correctly, all theLC_*
are set toen_US.UTF-8
exceptLC_TIME
that is set toit_IT.utf8
. I didn't like it because I get 24-hours format but also the date is translate in Italian. I set theLC_TIME
toC.UTF-8
and now all the locale parameters are set correctly and the format for date/time is correct. Thank you very much for your help. -
Jason Campbell over 2 yearsRather than replacing all the
%r
s with%T
s you can also adjust the definition of%r
itself, a few lines lower down in the same file. For instance, I was able to change%I
to%H
just once, in the line below the comment "Appropriate date representation (%r)", and saw a change to 24 hour time in all contexts I've tried so far