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.
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
An easier to read version of the man page can be found on Ubuntu Man Pages
Time as the command line prompt:
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 #:
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:
$ date Tue Jun 9 18:04:30 EEST 2015
zdumpused to echo the time in a specified time zone.
$ zdump EEST EEST Tue Jun 9 15:05:17 2015 EEST
$ sudo hwclock Tue 09 Jun 2015 06:05:55 PM EEST -0.656710 seconds
clock but needs to install xview-clients
sudo apt-get install xview-clients
ntpdateis used to set system time but using without
sudowill just print the time and date.
$ ntpdate 26 Jun 10:48:34 ntpdate: no servers can be used, exiting
With Ubuntu 15.04 (systemd) there is also
timedatectl which shows you the time and allows you to change it and more in
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.
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:
(The screenshot is on a virtual terminal, but it's the same on a virtual console).
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
localtime()function gives the
structdatatype 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.
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:
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