4

I am looking to have my date command show time:

mohi@DESKTOP-PM4LGGS:~/Dropbox/mpl$ date
Mon, Jul 30, 2018  3:31:41 PM

like I am seeing in my Git Bash on my office Windows. How can I get my date command show AM/PM time by default on my home Ubuntu 18.04 LTS (GNOME Terminal)?

don_crissti
  • 82,805

3 Answers3

10

Supplying date with an explicit format string would do this:

$ date +'%a, %b %d, %Y  %r'
Mon, Jul 30, 2018  11:45:58 AM

or

$ date +'%a, %b %d, %Y %l:%M:%S %p'

where %l:%M:%S %p is a bit more locale-independent than %r might be.

As a shell function that overloads date with this format only when called without any options:

date () {
    [ "$#" -eq 0 ] && set -- +'%a, %b %d, %Y  %r'
    command date "$@"
}

You would execute the function definition as written above directly in your interactive shell to make it available there, or put it wherever you ordinarily put aliases.

Kusalananda
  • 333,661
  • 3
    Note that the expansion of %r is locale-dependent and is not guaranteed to contain AM/PM. For instance here on a GNU system, I get 11:56:02 am BST in a British locale and 11:56:02 in a French locale. in the C/POSIX locale, you'd get 01:23:44 PM instead of 1:23:44 PM. – Stéphane Chazelas Jul 30 '18 at 11:00
0

Use:

localectl set-locale LC_TIME=en_US.UTF-8
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • That has the disadvantage that it puts day and month wrong way around (though the example in the question suggests that's probably acceptable). – Toby Speight Jan 15 '22 at 13:58
-3

You can add your preferred command as an alias to the ~/.bash_aliases or ~/.bashrc file.

alias d='date +"%a, %h %d, %Y %r"'

For the change to take effect, you have to run:

source ~/.bash_aliases

Then, you can use d to get the date in your desired format.

Ipor Sircer
  • 14,546
  • 1
  • 27
  • 39