- How to get date and time using command line interface?
- 9 Answers 9
- How can I get the current date and time in the terminal and set a custom command in the terminal for it? [closed]
- How to Work with Date and Time in Bash Using date Command
- Change Linux System Date and Time
- Formatting Options
- Handling Date in Linux
- Handling Time in Linux
- With –date or -d Flag
- Common Operations
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 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».
How to Work with Date and Time in Bash Using date Command
Date command is an external bash program that allows to set or display system date and time. It also provides several formatting options. Date command is installed in all Linux distros by default.
Type date command in terminal which will display current date and time.
Change Linux System Date and Time
Using date command, system date, time and timezone can be modified and the change has to be synced with the hardware clock.
$ date --set="Thu Nov 12 13:06:59 IST 2020" $ hwclock --systohc
Formatting Options
A good place to get the list of formatting options will be the man page.
Let’s see some of the most common formatting options that we will use.
- To apply formatting use “+ followed by “formatter“.
- To get a list of formatting options for GNU\LINUX take a look at the linked man page.
- To get a list of formatting options for BSD take a look at the linked man page.
The two important parts of the date command is using Format +% and –date option.
Now let’s apply some formatting on the date command. To apply formatting, add plus sign (+) followed by %formatter as shown in examples.
Handling Date in Linux
Let’s take a look at how to use date related formatters in a simple shell script called ‘date.sh‘.
# PRINT YEAR,MONTH,DAY AND DATE. echo "We are in the year = $(date +%Y)" echo "We are in the year = $(date +%y)" # Difference between %Y and %y is %Y will print 4 digits while %y will print the last 2 digits of the year. echo "We are in the month = $(date +%m)" echo "We are in the month = $(date +%b)" echo "We are in the month = $(date +%B)" # Difference between %B and %b is, %B will print full month name while %b will print abbreviated month name. echo "Current Day of the month = $(date +%d)" echo "Current Day of the week = $(date +%A)" echo "Current Day of the week = $(date +%a)" # Difference between %A and %a is, %A will print full Weekday name while %a will print abbreviated weekday name. # Instead of formatting to get the date, we can use %D which will print the date as %m/%d/%y or %F which prints in %Y-%M-%d format. echo "Date using %D = $(date +%D)" echo "Date using %F = $(date +%F)"
Handling Time in Linux
Let’s take a look at how to use time related formatters in a simple shell script called ‘time.sh‘.
# PRINT HOURS, MINS, SECONDS, NANO SECONDS echo Hours = $(date +%H) echo Minutes = $(date +%M) echo Seconds = $(date +%S) echo Nanoseconds = $(date +%N) echo Epoch Time = $(date +%s) echo "current time = $(date +%H:%M:%S:%N)" # can also use %T which displays Time in HH:MM:SS format. echo "current time in 24 hour format = $(date +%T)" # can also use %r to display time in 12 hour format. echo "current time in 12 hour format = $(date +%r)"
With –date or -d Flag
With —date or -d flag input can be passed as string and date command knows to handle it smartly.
Let’s see some examples to understand how it works.
# Print yesterday's date and time. echo "Yesterday = $(date -d "Yesterday")" # Print Tomorrow date and time. echo "tomorrow = $(date -d "tomorrow")" # Find what is the date and time before 10 days from now. echo "Before 10 days = $(date -d "tomorrow -10 days")" # Find last month and next month echo "Last month = $(date -d "last month" "%B")" echo "Next month = $(date -d "next month" "%B")" # Find last year and next year echo "Last Year = $(date -d "last year" "+%Y")" echo "Next Year = $(date -d "next year" "+%Y")" # Forecast the weekday echo "2 days away from today and it comes on weekdays? = $(date -d "Today +2 days" "+%A")
Common Operations
calculate the number of days between 2 given dates.
$ echo $(( ( $(date -d "2020-11-10" "+%s") - $(date -d "2020-11-01" "+%s") ) / 86400))
Find the given year is leap year or not.
$ for y in ; do date -d $y-02-29 &>/dev/null && echo $y is leap year; done
Assigning output of date command to a variable.
$ TODAY=$(date +%Y-%m-%d) OR $ TODAY1=$(date +%F) $ echo $TODAY $ echo $TODAY1
Create log files with the date added to the filename.
Adding date and time while creating log files, backup, or text files is a common operation that we will encounter most often. Let’s take an example, to take a backup, we have created a shell script.
This script will take a backup from 00:00 to 23:59 and scheduled to run daily at 00:00 of the next day. We want to create log files with yesterday’s date format.
CUSTOM_FORMAT=$(date —date «Yesterday» «+%d-%y-%H:%M») LOG_FILE=/var/log/custom_application/application_$.log echo «Script started» >> $ . CODE BLOCKS . echo «Script completed» >> $
That’s it for this article. In this article, we have seen how to use bash date and time in Linux. Let us know your feedback.