8 examples of using date commands

Time:2021-11-25

The date command is used to display and set date and time settings on Linux systems. This tutorial briefly describes how to use the date command to display and set dates on Linux systems.
Date command without options
The date command does not add any command options. It displays the current date and time, including day of week, month, year, HThe time and time zone in s format are as follows.

[[email protected] ~]# date
Mon May 24 14:50:31 CST 2021
8 examples of using date commands 8 examples of using date commands

Display in world standard time
To display the time in UTC, you need to add the – U option.

[[email protected] ~]# date -u
Mon May 24 06:51:21 UTC 2021
8 examples of using date commands 8 examples of using date commands

Displays a specific date in string format
You can use the — date option to display a specific date in string format, which will not affect the date and time of the system, but only convert the date format to string:

[[email protected] ~]# date –date=”5/20/2021 13:14″
Thu May 20 13:14:00 CST 2021
8 examples of using date commands 8 examples of using date commands

Use the date command to check the past date
The date command can also print past dates and times relative to the current date. For example, to view the date 9 days ago, run the command:

[[email protected] ~]# date –date=”9 days ago”
Sat May 15 14:56:47 CST 2021
8 examples of using date commands 8 examples of using date commands
Check the date three months ago:

[[email protected] ~]# date –date=”3 months ago”
Wed Feb 24 14:57:29 CST 2021
8 examples of using date commands 8 examples of using date commands
Check what the date is today two years ago:

[[email protected] ~]# date –date=”2 years ago”
Fri May 24 15:02:40 CST 2019
8 examples of using date commands 8 examples of using date commands

Use the date command to check for future dates
The date command can also display the future date, for example, to see what tomorrow’s date is:

[[email protected] ~]# date –date=”tomorrow”
Tue May 25 14:58:49 CST 2021
8 examples of using date commands 8 examples of using date commands
To check the exact date a week from now, run:

[[email protected] ~]# date –date=”next week”
Mon May 31 14:59:27 CST 2021
8 examples of using date commands 8 examples of using date commands
Show date after 3 weeks:

[[email protected] ~]# date –date=”3 weeks”
Mon Jun 14 15:00:11 CST 2021
8 examples of using date commands 8 examples of using date commands
To display the date after 4 months:

[[email protected] ~]# date –date=”4 months”
Fri Sep 24 15:01:42 CST 2021
8 examples of using date commands 8 examples of using date commands
Show today’s date two years later:

[[email protected] ~]# date –date=”2 years”
Wed May 24 15:03:32 CST 2023
8 examples of using date commands 8 examples of using date commands

Options for the date command
The date command comes with a number of options and lists some of the formatting options available.

%D – date display format is month / day / year
%Y – year (e.g. 2021)
%M – month (01-12)
%B – full name of the month (e.g. January)
%B – short name of the month (e.g. Jan)
%D – which day of the month (e.g. 01)
%J – the day of the year (001-366)
%U – the day of the week (1-7)
%A – day of the week (e.g. Friday)
%A – short name of the day of the week (e.g. Fri)
%H – hour, 24-hour system (00-23)
%I – hour, 12 hour system (01-12)
%M – minutes (00-59)
%S – seconds (00-60)
The syntax for using the date option is very simple:

date “+%option”
For example, to print dates in YY / mm / DD format, run:

[[email protected] ~]# date “+%Y/%m/%d”
2021/05/24
8 examples of using date commands 8 examples of using date commands
To print the day of the week, month, day, year, and current time:

[[email protected] ~]# date “+%A %B %d %Y %T”
Monday May 24 2021 15:24:07
How to set date and time
The date command also allows you to set the date and time. For example, to set the date and time to 11:15 a.m. on June 25, 2021, run the following command:

[[email protected] ~]# date –set=”20210625 11:15″
Fri Jun 25 11:15:00 CST 2021
Using the date command in a variable
When creating a shell script, we save the date command to a variable, and then use the variable to create a log file. The example is as follows:

!/bin/bash

LOGFILE=/tmp/logs-$(date +%d-%m-%Y_%T)
echo “##Check Cluster for Failed Resources##” >> $LOGFILE
crm_mon -1 -rf | grep FAILED >> $LOGFILE
echo -e “\n\n” >> $LOGFILE
echo “##Check Cluster for Stopped Resources##” >> $LOGFILE
crm_mon -1 -rf | grep -i STOPPED >> $LOGFILE
echo -e “\n\n” >> $LOGFILE
summary
The date command is used to display and set date and time settings on Linux systems. This tutorial briefly describes how to use the date command to display and set dates on Linux systems.

Recommended Today

Apache sqoop

Source: dark horse big data 1.png From the standpoint of Apache, data flow can be divided into data import and export: Import: data import. RDBMS—–>Hadoop Export: data export. Hadoop—->RDBMS 1.2 sqoop installation The prerequisite for installing sqoop is that you already have a Java and Hadoop environment. Latest stable version: 1.4.6 Download the sqoop installation […]