How to get date and time using command line interface?

The date command will show you the date and time.

man date will show you how to control the output to whatever format you need, if you want something other than the standard output. For example:

date "+%H:%M:%S   %d/%m/%y"

will output the date and time in the format 14:09:22 09/06/2015

An easier to read version of the man page can be found on Ubuntu Man Pages

Time as the command line prompt:

PS1="\@ \w$bk"

Looks like: 11:41 AM ~

A little fancier, enclosed in brackets:

PS1="[ \@ \w$bk ]"

Looks like: [ 11:36 AM ~ ]

Add the user and localhost with the time, all within brackets:

PS1="[ \u on \h \@ \w$bk ]"

Looks like: [ DrPete on LittleSorrel 10:34 AM ~ ]

Reset prompt to default #: PS1="# "

To make the prompt permanently available, add the prompt line of your choice, i.e.,

export PS1="\@ \w$bk "

to ~/.profile .

Can't help it, we are rolling now... add colors, define them in .profile to make them easier to set up:

 # Install GNU coreutils
    bk="\[\033[0;38m\]" #means no background and white lines
    txtBlue="\[\033[0;34m\]"   #letter color blue
    txtRed="\[\033[0;31m\]"    #letter color red
    txtCyan="\[\033[1;36m\]"   #letter color cyan
    txtWhite="\[\033[1;37m\]"  #letter color white
    txtYellow="\[\033[1;33m\]" #letter color yellow

Then a superfancy colored prompt would be:

PS1="[ $txtYellow\u on $txtCyan\h $txtRed\@ $txtWhite\w$bk ]"

There are a list of command used for time and date:

  1. The command date

    $ date 
    Tue Jun  9 18:04:30 EEST 2015
  2. The command zdump used to echo the time in a specified time zone.

    $ zdump EEST
    EEST  Tue Jun  9 15:05:17 2015 EEST
  3. hwclock

    $ sudo hwclock
    Tue 09 Jun 2015 06:05:55 PM EEST  -0.656710 seconds
  4. clock but needs to install xview-clients

    sudo apt-get install xview-clients
  5. using ntpdate command. ntpdate is used to set system time but using without sudo will just print the time and date.

    $ ntpdate
    26 Jun 10:48:34 ntpdate[4748]: no servers can be used, exiting


Command Line