The Date command is an external bash program that allows you to set or display the date and time of the system. It also offers various layout options. The Date command is installed by default in all Linux distributions.
which date $
type – date
Date of study team location
Enter the Date command into the terminal, which displays the current date and time.
Date
Checking the date under Linux
Changing the date and time of the Linux system
Use the date command to change the date, time and time zone of the system and the change must be synchronized with the hardware clock.
date – installation=Thu November 12 13:06:59 ACTUAL 2020
$ time zone – systochk
Setting the date and time of the Linux system
Layout variants
A good place to get a list of formatting options is the manual page.
Man Date
Let’s take a look at some of the more common formatting options we will use.
- To apply the layout, use the + then the layout.
- A list of GNULINUX formatting options can be found on the manual links page.
- For a list of BSD formatting options, see the man page link.
The two main parts of the Date team use the Format +% and Date options.
Now let’s apply some formatting to the date command. To apply the formatting, add the plus sign (+) and then the % formatting as in the examples.
Processing date on Linux
Let’s see how to use date formatters in a simple shell script called date.sh.
# PRINT THE YEAR, THE MONTH, THE DAY AND THE DATE… # #
echo We are in the year = $(date +%Y)
echo We are in the year = $(date +%y)
# The difference between %Y and %y is that %Y prints 4 digits, while %y prints the last 2 digits of the year.
echo We in months = $(date +%m)
echo We in months = $(date +%b)
echo We in months = $(date +%B)
# The difference between %B and %b is the same, %B prints the full name of the month, while %b prints the short name of the month.
ultrasound Day of current month = $(date +%d)
echo Current weekday = $(date +%A)
echo Current weekday = $(date +%A)
# The difference between %A and %a is the same, %A prints the full name of the day of the week, while %a prints the short name of the day.
# Instead of formatting the date, we can use %D, which prints the date in %m/%/%y format, or %F, which prints the date in %Y-%M-%d format.
echo Date with %D = $(Date +%D)
echo Date with %F = $(Date +%F)
Scenario search Date and time
Processing time under Linux
Let’s see how we can use time-based trainers in a simple shell script called time.sh.
# PRINT HOURS, MINUTES, SECONDS, NANOSECONDS #
echo hour = $(Date +%H)
echo minutes = $(Date +%M)
echo seconds = $(Date +%S)
echo nanoseconds = $(Date +%N)
echo time = $(Date +%S)
current echo time = $(date +%H:%M:%S:%N)
# can also use %T, which displays the time in HH:MM:SS format.
Echo of the current time in 24-hour format = $(date +%T)
# can also use %r to display the time in 12-hour format
Echo of the current time in 12-hour format = $(date +%r)
s -Date or -d Flag
The -date or -d flag can be passed as a string, and the date command knows how to handle it correctly.
Let’s take a look at some examples to see how this works.
# Print yesterday’s date and time
echo Yesterday = $(Date -d Yesterday)
~ Print tomorrow’s day and time ~
Echo tomorrow = $(date -d tomorrow)
# Find the date and time 10 days ago #
Echo 10 days ago = $(date -d tomorrow -10 days)
#
Echo of previous month = $(Date -d of previous month %B)
Echo of next month = $(Date -d of next month %B)
#
Echo previous year = $(Date -d previous year +%Y)
Echo next year = $(Date -d next year +%Y)
# The forecast for weekday
sounds two days from today and comes on weekdays… # = $(Date -d Today +2 days +%A)
Check the date by formatting
General operations
to calculate the number of days between two given dates.
$ echo $((((date -d 2020-11-10 +%s) – $(date -d 2020-11-01 +%s))) / 86400))
Figure it out this year, leap year or not.
for y in {2000…2020} ; do date -d $y-02-29 &>/dev/null && echo $y is leap year ; done
Finding the leap year on Linux
Assign variable date control output.
$ TODAY=$ (Date +%Y-%m-%d)
OF
$ TODAY1=$ (Date +%F)
$ Echo $ TODAY
$ Echo $ TODAY1
Variable Date Allocation
Create log files with a date added to the filename.
Adding a date and time when creating log files, backups or text files is a common operation we most often encounter. Let’s take an example: To back it up, we created a shell script.
This script is saved from 00:00 to 23:59 and executed daily at 00:00 the next day. We want to create log files in yesterday’s date format.
CUSTOM_FORMAT=$(date – date yesterday +%d-%%%%-H:%M)
LOG_FILE=/var/log/custom_application_${CUSTOM_FORMAT}.log
Start the Echo script >> ${LOG_FILE}
.
CODE BLOCKS
…
Scenario Echo completed >> ${LOG_FILE}
That’s it for this article. In this article we have seen how to use bash date and time under Linux. Let us know what you think.
If you like what we do here at TecMint, you should think:.
TecMint is the fastest growing and most reliable community site for all articles, guides and books about Linux on the web. Millions of people visit TecMint! to find or consult thousands of published articles that are accessible to everyone for FREE.
If you like what you read, consider buying us a coffee (or 2) as a thank you.
Thank you for your continued support.
Related Tags:
unix date format yyyymmddhhmmss,linux date format milliseconds,unix date command examples,date conversion in bash,get yyyy mm dd bash,bash string to date,bash date minus 1 day,bash date milliseconds,bash date –iso,bash date math,bash current timestamp,bash get current time in milliseconds,bash get yesterday date,time to string bash,bash date time var,linux system time command,linux terminal clock,terminal time command,bash live clock,linux live clock,bash prompt dynamic time,how to compare time in shell script,bash time command,bash time elapsed,set date command in linux,linux date -1 day,cal command in linux,date s option,set time described by string,which command will find all read only files,linux "date -d",bash is an acronym for ________,bash parse date custom format,bash parse date to timestamp,bash parse date from string,bash date –iso 8601,bash get month number,bash date compare,how to get current date and time in shell script,how to change date format in unix shell script,how to store date in a variable in shell script,linux date command,linux date format yyyymmddhhmmss,time in unix command,bash date variable