How to get date and time using command line interface?
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
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:
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:
A little fancier, enclosed in brackets:
Add the user and localhost with the time, all within brackets:
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.,
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 ]"
My first answer — so now that I am no longer a virgin — expand in what way, narrative or link to CLI prompt info?
There are a list of command used for time and date:
$ date Tue Jun 9 18:04:30 EEST 2015
$ 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
sudo apt-get install xview-clients
$ ntpdate 26 Jun 10:48:34 ntpdate[4748]: 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 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.
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).
How can I display the current time & date setting?
I don’t know of a single file, but this may give you the info needed:
cat /etc/timezone grep UTC /etc/default/rcS date # hardware clock sudo hwclock --show
Best example (IMHO) using timedatectl (in command-line/terminal):
$ timedatectl Local time: Thu 2014-07-24 19:51:23 IST Universal time: Thu 2014-07-24 14:21:23 UTC Timezone: Asia/Kolkata (IST, +0530) NTP enabled: no NTP synchronized: no RTC in local TZ: no DST active: n/a
Visit the manpage for more settings and further information.
Additional info that may also be useful: to change time zone do «sudo timedatectl set-timezone America/Los_Angeles» (use your own time zone instead of America/Los_Angeles. Timezones can be found with timedatectl list-timezones)
Fri 22 Nov 2019 04:31:50 PM UTC
Check out info date , and for example date +’%z’
For the time zone, you can use geolocation:
$ curl https://ipapi.co/timezone America/Chicago
$ curl http://ip-api.com/line?fields=timezone America/Chicago
If you need a formatted area and time zone, you can use:
$ grep `date +%Z` /etc/timezone Etc/UTC
PS> Get-TimeZone Id : Europe/Vilnius DisplayName : (UTC+02:00) Eastern European Standard Time StandardName : Eastern European Standard Time DaylightName : Eastern European Summer Time BaseUtcOffset : 02:00:00 SupportsDaylightSavingTime : True
I suspect this won’t be popular answer in a Linux community, but I really like the verb-noun convention. It makes it easier for me to remember commands, and it will also work on all distributions with PowerShell installed 🙂
Linux Get Current Time
The time function is utilized within all operating systems like Windows, Linux, Unix, etc. You may see the current date and time at the desktop screens of your operating system within standard formats. But what about the exact current time displayed on the Linux operating system. If you are looking for a guide toward using date and time functions to display the current date and time in the Linux shell, then this article is for you. So let’s start this guide by logging in to your Ubuntu 20.04 system, as we will be performing each command on the Ubuntu 20.04 Linux shell.
After a successful login, you first need to open the Ubuntu’s terminal shell via the activity area at the desktop taskbar. Tap on it and write “terminal” in the search bar shown on your screen. The pop-up terminal screen will be shown, and you have to tap on it to open it quickly. If this process is lengthy, try using the “CTRL+Alt+T” to launch it faster. Now, your terminal will be opened within no more than 10 seconds on your screen. Let’s start with the most basic command to display the date and time of our current time zone on the shell. On executing the following command, the time has been displayed in the “hour: minute: second” format along with the time zone, i.e., PKT. It also shows the current date according to the time zone. Upon execution, you will get the output, as shown below:
If you want to only display the date on your shell with the specific format, you need to specify the format in the date command. Use the inverted commas to add the format in “%d” for day, %m for month, %y for year separated by “-” signs. This command execution shows us the date in standard “day-month-year” format. Upon execution, you will get the following output:
If you only want to display the current time on your shell using the “date” command, you must use the “+%T” character flag. On execution of this command, the current time for a specific time zone will be displayed in a standard format, i.e., “hour: minute: second”. Upon execution, you will get the output, as shown below:
If you want to display both the current and time in a single line with the time and date specification, you can also do that with the date command. So, to display the title “Date” and “Time”, we need to add “+DATE: %D” for date and “TIME: “%T” for time. The output of this instruction shows the date and time in standard format with titles of date/time on the shell. Upon execution, you will get the following output:
For example, we want to get the exact date and time of the same time zone and same time for some past year. We need to utilize the “—date” flag with the “=” sign to get the value to be searched. For example, we want to get a date and time exactly three years back for the same moment. On execution, the following instruction shows the date and time for the exact three years back, i.e., February 27, 2019:
Upon execution, you will get the affixed output.
If we want to take an exact date and time for the very next day on the shell, we will use the same “date” command with the “—date” flag. Use the value “tomorrow” in the inverted commas and execute the command. This will show the next exact date from the current exact date and time, i.e., February 28, 2022.
Upon execution, you will get the following output:
Upon execution, you will get the following output:
Upon execution, you will get the following output:
Upon execution, you will get the following output:
Upon execution, you will get the following output:
Another command is also known to get the current date and time for the current time zone of Linux. This command is the “timedatectl” instruction of Bash. It will not only show you the current local time but also the universal time, RTC time, your current time zone, and if the NTP services is enabled on your system. The execution of this command shows all the mentioned specifications on the shell, i.e., time and date. Upon execution, you will get the output, as shown below:
Let’s use the time zone date command to get the current time of our choice of time zone. So, we have to use the “TZ” variable with the “=” sign to get the time zone value. We want to get the current time for “Asia/Karachi” this time. The use of the “date” keyword with the “+%T” is necessary for fetching time for this time zone. We have the time displayed for the “Asia/Karachi” time zone upon execution. If you want to get the value for another time zone, use the specific time zone as a value to TZ. Let’s say we have been using the time zone “Asia/Istanbul” to get the current time for Istanbul, Turkey. The instruction shows the time for “Istanbul, Turkey” on the shell. Upon execution, you will get the following output:
$ TZ = “Asia / Karachi” date “+ % T”
$ TZ = “Asia / Istanbul” date “+ % T”
Conclusion:
In this article, we have tried to implement almost all the commands to get the current date and time for our current time zone. We have also tried to get the current time for other time zones, the past time and date, the coming date and time, and many more. You can also use %r and %R to get the current time. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.
About the author
Omar Farooq
Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.
How can I get the current date and time in the terminal and set a custom command in the terminal for it? [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
I have to check the time in a Linux terminal. What is the command for getting date and time in a Linux terminal? Is there a way in which we can set a custom function?
This question seems on topic as it relates to a specific computing task on a specific operating system (which is just a software layer itself). As, @drstevens said, this is the first result in Google and it was informative for me needs. If it were off-topic, it would read something like «Do operating systems tell time?» or «What do you guys think about operating systems that tell time.» Stack Overflow is a great resource for beginners and it seems like this was closed as for being too beginner of a question. It should instead just be listed as a beginner question.
StackExchange is the place for answers. This is a good question. Perhaps the question should be migrated, but it should not be closed, in my opinion. This question appeared first in my Google results when I searched for «linux check time».