3

So I have 2 different Linux installations. One of them is Ubuntu and the second one is Kali.

When I run date command with no options/arguments on my Ubuntu install I get:

michal@ubuntu:~$ date
Thu 24 Mar 2022 07:56:23 PM CET

When I run date command with no options/arguments on my Kali install I get:

┌──(michal㉿kali)-[~]
└─$ date
Thu Mar 24 07:58:34 PM CET 2022

The locale setting is the same on both machines being: Ubuntu locale settings:

michal@ubuntu:~$ locale
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=

and Kali locale settings:

┌──(michal㉿kali)-[~]
└─$ locale
LANG=en_US.UTF-8
LANGUAGE=en_US:en
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=

Why the date command output is different on both machines?

I want to PERMANENTLY change the Kali output, to be the same as my current Ubuntu output being:

michal@ubuntu:~$ date
Thu 24 Mar 2022 07:56:23 PM CET

Which file needs to be edited? Where are those settings?

I've tried to follow steps from this thread: How can I change the default date format (using LC_TIME)? but I don't understand what: "date's texinfo also explicitly recommends to set LC_TIME to C in order to produce locale independent output." means.

2
  • 1
    Are they both running the same date (coreutils) version? I see a similar difference on two Ubuntu systems between coreutils 8.28 and 8.30 (the 8.28 version doesn't appear to honor the format shown by locale -c d_t_fmt unless I add the +%c specifier explicitly) Mar 24, 2022 at 20:47
  • 1
    The last bit in your question means setting the LC_TIME variable, like LC_TIME=C date (which only changes it temporarily for that command).
    – Panki
    Mar 24, 2022 at 21:46

2 Answers 2

3

You can tell date how it should format its output:

%%   a literal %
  %a   locale's abbreviated weekday name (e.g., Sun)
  %A   locale's full weekday name (e.g., Sunday)
  %b   locale's abbreviated month name (e.g., Jan)
  %B   locale's full month name (e.g., January)
  %c   locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
  %C   century; like %Y, except omit last two digits (e.g., 20)
  %d   day of month (e.g., 01)
  %D   date; same as %m/%d/%y
  %e   day of month, space padded; same as %_d
  %F   full date; like %+4Y-%m-%d
  %g   last two digits of year of ISO week number (see %G)
  %G   year of ISO week number (see %V); normally useful only with %V
  %h   same as %b
  %H   hour (00..23)
  %I   hour (01..12)
  %j   day of year (001..366)
  %k   hour, space padded ( 0..23); same as %_H
  %l   hour, space padded ( 1..12); same as %_I
  %m   month (01..12)
  %M   minute (00..59)
  %n   a newline
  %N   nanoseconds (000000000..999999999)
  %p   locale's equivalent of either AM or PM; blank if not known
  %P   like %p, but lower case
  %q   quarter of year (1..4)
  %r   locale's 12-hour clock time (e.g., 11:11:04 PM)
  %R   24-hour hour and minute; same as %H:%M
  %s   seconds since the Epoch (1970-01-01 00:00 UTC)
  %S   second (00..60)
  %t   a tab
  %T   time; same as %H:%M:%S
  %u   day of week (1..7); 1 is Monday
  %U   week number of year, with Sunday as first day of week (00..53)
  %V   ISO week number, with Monday as first day of week (01..53)
  %w   day of week (0..6); 0 is Sunday
  %W   week number of year, with Monday as first day of week (00..53)
  %x   locale's date representation (e.g., 12/31/99)
  %X   locale's time representation (e.g., 23:13:48)
  %y   last two digits of year (00..99)
  %Y   year
  %z   +hhmm numeric time zone (e.g., -0400)
  %:z  +hh:mm numeric time zone (e.g., -04:00)
  %::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)
  %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)
  %Z   alphabetic time zone abbreviation (e.g., EDT)

In your case the command would be as following:

date +"%a %d %b %Y %r %Z"

By setting an alias, you can change the behavior of the date command:

alias date='date +"%a %d %b %Y %r %Z"'

You can put the alias in your ~/.bashrc to make the change permanent for your current user.

2

The default format for the date(1) command is buried deep in the locale system (specifically, the date_fmt setting in LC_TIME). Locale is the system for specifying location-dependent stuff, like what day of the week comes first, paper size, formats of telephone numbers, whether it's d/m/y or m/d/y and how many digits in each, etc.

The date(1) default changed for en_US (English in the United States) users in 2018, just AFTER Ubuntu 18.04LTS came out. In the US, the date(1) command produces dates with a 12-hour clock and AM or PM in them in Ubuntu versions later than 18.04. I don't know what other distros did or when.

The C/POSIX locale is a location-independent locale guaranteed never to change. It's pretty much what Unix systems did before they started adjusting for locales. Its locale is called C.

You have several options, depending on your situation:

  1. If you have no software depending on old data that used the date(1) command, you could consider just living with it. It is in fact the case that in the US, people use a 12-hour clock and AM/PM.

  2. If, like me, you want to continue using date(1) in scripts to produce the date in a specific format, you could use a format specifier in the command line:

    $ date +"%a %b %e %H:%M:%S %Z %Y"
    Mon Jun 12 15:30:44 EDT 2023
    
  3. You can also specify the locale to use with the LANG environment variable, and set that to C:

    $ LANG=C date
    Mon Jun 12 15:30:44 EDT 2023
    

    That can be set for a given shell with

    $ export LANG=C
    $ date
    Mon Jun 12 15:30:44 EDT 2023
    

    If you want it for every shell, put the export LANG=C line in ~/.bash_aliases.

  4. If you want date(1) to produce a given format without arguments, even when called by other users or not from a shell, then you need to change the system default format for the date(1) command. It's a little more involved:

    # Modify one line in the en_US locale file:
    cd /usr/share/i18n/locales
    sed -e '/date_fmt/ s/%r/%H:%M:%S/'  en_US > en_US@Cdate
    
    # List it as a supported locale:
    cat >> /etc/locale.gen << EOF
    [email protected] UTF-8
    EOF
    
    # compile the locale database
    locale-gen
    
    # select the new locale (modifies /etc/default/locale)
    update-locale [email protected]
    

This takes effect on new logins.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .