190

How can I get current date/time in terminal. I mostly start up my system in text(console) mode and need the command to know the current date/time.

9 Answers 9

302

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

2
  • 17
    +1 just for saying man date Jul 1, 2015 at 5:31
  • 2
    how can i get timezone difference? Aug 20, 2019 at 2:55
21

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 ]"
10
  • very good answer =) but a little bit short.
    – A.B.
    Jun 9, 2015 at 15:42
  • My first answer - so now that I am no longer a virgin - expand in what way, narrative or link to CLI prompt info?
    – drPete
    Jun 9, 2015 at 15:48
  • How can I make this permanent, eg.
    – A.B.
    Jun 9, 2015 at 15:49
  • just export @A.B. Clever Answer btw :) Jun 9, 2015 at 15:51
  • 1
    Better answer = more upvotes
    – A.B.
    Jun 9, 2015 at 15:52
21

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
    
12

With Ubuntu 15.04 (systemd) there is also timedatectl which shows you the time and allows you to change it and more in man timedatectl.

Without arguments it gives

% timedatectl
Warning: Ignoring the TZ variable. Reading the system's time zone setting only.

      Local time: ke 2015-06-10 10:31:59 EEST
  Universal time: ke 2015-06-10 07:31:59 UTC
        RTC time: ke 2015-06-10 07:31:59
       Time zone: Europe/Helsinki (EEST, +0300)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: yes
 Last DST change: DST began at
                  su 2015-03-29 02:59:59 EET
                  su 2015-03-29 04:00:00 EEST
 Next DST change: DST ends (the clock jumps one hour backwards) at
                  su 2015-10-25 03:59:59 EEST
                  su 2015-10-25 03:00:00 EET

And another nice feature that I have used is timedatectl set-ntp true which activates systemd-timesyncd which is inbuild SNTP (Simple Network Time Protocol) client which syncs your clock with remote server.

1
  • 1
    $ timedatectl works fine here on Ubuntu 14.04 as well.
    – Doorknob
    Jun 10, 2015 at 14:14
8

Although all the answers here are correct, you need to use the date command manually to see the time whenever you want, and the output will mix with normal output of your command. This is mostly ok, but sometime is not practical.

You can add the date command to your prompt which will show a (fixed) clock every time the shell is ready for the command, or you can use one of the terminal multiplexer that have a status line. My preferred one (although I admit the oldest one) is GNU screen.

In console or in a terminal, say via ssh, if you use screen, among zillions of different things you can do (I used to think about screen as unavoidable for working when I had just a text terminal) you have the option --- which is normally in the default configuration --- of having a clock in the status line:

Screen on a terminal

(The screenshot is on a virtual terminal, but it's the same on a virtual console).

1
  • 1
    Byobu (which is based around screen) also does that out of the box. Just FYI Jan 6, 2017 at 21:17
5

Also will output the date and time:

sudo hwclock

More detailed:

man hwclock
1
2

There was no mention of python, so I'll throw it out here:

$ python -c 'import time;print(time.asctime(time.localtime()))'                                                                       
Tue Nov 29 20:25:27 2016

The way it works is quite simple:

  • we import time module
  • localtime() function gives the struct datatype with all the information needed, such as time of day, day, year,etc.
  • asctime() converts all that information to string, and we print it out nicely formatted

Works in python 2 and 3 alike.

1

if you are looking for sometting like YYYYMMDDHHMMSS, 20160804020100 use this:

date +%Y%m%d%H%M%S

it servers most purposes like file backup, or log filtering.

2
  • 2
    Welcome to AskUbuntu. While this answer does attempt to solve posted question, the solution has already been mentioned in the answer by multiple other users. Personally I don't find this answer useful. Please improve your post Nov 30, 2016 at 3:31
  • This is ideal to view log files. For example, you want to see today's log and command like sudo cat /var/logs/app/log$(date +%Y%m%d).log will open todays records. Jul 4, 2022 at 19:40
0

If you're not sure about the accuracy of your system's clock, or you want to use an external time source you can obtain the time over the Internet. For best accuracy you can use sntp (installed by default on MacOS and comes as part of the ntp distributions) to obtain the date:

sntp pool.ntp.org 

If you only have web access to the internet then it can be obtained over HTTP using HTP which is available on many systems as htpdate which usually only has accuracy to the nearest second, or you can access it using TLS (which extracts the timestamp from the TLS exchange) tlsdate which can provide better accuracy:

tlsdate -nV ntp.org

You must log in to answer this question.

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