Ubuntu server 20.04 - time format 24 hours on shell with date command

11,658

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.

Share:
11,658

Related videos on Youtube

MadHorse
Author by

MadHorse

Updated on September 18, 2022

Comments

  • MadHorse
    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
    MadHorse almost 4 years
    Hi 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 the LC_* 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
    Mateusz almost 4 years
    Hi, C is default locale when no locale is set, and it should not cause any problems. I suppose that now you see C value because you do not have any locale set to main LANG variable. To check your current configuration see file /etc/default/locale. If you want, you can set LANG by running localectl set-locale en_US.UTF-8 or by manually editing config file.
  • Mateusz
    Mateusz almost 4 years
    24-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
    MadHorse almost 4 years
    Thank 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. The LANG variable at the beginning was set to en_US.UTF-8, with the command localectl set-locale LC_TIME=it_IT.UTF-8 only the variable LC_TIME had to change, but all the variables LC_TIME included, changed to C.UTF-8. Shouldn't just change only the LC_TIME variable and leave the others variables unchanged ?
  • Mateusz
    Mateusz almost 4 years
    Your understanding is correct. localectl should have just add definition for LC_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 original LANG value, but unfortunately I do not know why. This would explain that all LC_* changed its value. Normally all LC_* variables are not explicitly defined. You define one of them when you want to override some locale setting. When some LC_ variable is not defined the locale command will show it with value inherited from LANG.
  • MadHorse
    MadHorse almost 4 years
    Ok, I found the problem, with the command locale -a I have a list of all the avialable locale and there wasn't the it_IT.utf8 so i use the command locale-get it_IT.utf8 to generate the locale and then rebooted. Now I have all the locale setting correctly, all the LC_* are set to en_US.UTF-8 except LC_TIME that is set to it_IT.utf8. I didn't like it because I get 24-hours format but also the date is translate in Italian. I set the LC_TIME to C.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
    Jason Campbell over 2 years
    Rather than replacing all the %rs with %Ts 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