Learn a Linux command every day (50): Date

Time:2021-9-26

Learn a Linux command every day (50): Date

Yesterday’s recommendation:Learn a Linux command every day (49): Free

Command introduction

The date command is used to display and set the system time.

Syntax format

date [OPTION] [MMDDhhmm[[CC]YY][.ss]]

Option description

-D < string >   # Displays the date and time to which the string refers. The string must be preceded by double quotes
-S < string >   # Set the date and time based on the string. The string must be preceded by double quotes
-u   # Show GMT
--help      # Print online help information
--version   # Print version information

Time string list

FORMAT controls the output.  Interpreted sequences are:
%%  # Character%
%H  # Hour, 24-hour system (Representation: 00 ~ 23)
%I  # Hour, 12 hour system (expression: 01 ~ 12)
%k  # Hour, 24-hour system (expression: 0 ~ 23)
%l  # Hour, 12 hour system (expression: 1 ~ 12)
%M  # Min (00 ~ 59)
%p  # Show morning or afternoon
%r  # Time, 12 hour system
%s  # Number of seconds from 0:00 on January 1, 1970 to now
%S  # Seconds (00 ~ 59) 
%T  # Time (24-hour system) (HH: mm: SS)
%X  # Format of display time (% h hours% m minutes% s seconds)
%Z  # Alphabetical time zone abbreviations
%a  # Abbreviation of week name
%A  # Full name of Sunday
%b  # Month name abbreviation
%B  # Full name of month
%c  # Date and time
%d  # Date by month (01-31)
%D  # Date (mm / DD / yy) 
%h  # Same as% B option
%j  # The day of the year (001 ~ 366)
%m  # Month (01 ~ 12)
%w  # The day of the week (0 for Sunday)
%W  # The first few weeks of the year (00 ~ 53, Monday is the first day)
%x  # Format of display date (mm / DD / yy)
%y  # The last two digits of the year (99 in 1999)
%Y  # Year (e.g. 1970, 1996, etc.)
%C  # Century, usually omitting the last two digits of the current year
%U  # The week of the year, with Sunday as the first day of each week
%e  # Date by month, add space, equal to%_ d

Application examples

Format and output current time information

[[email protected] ~]# date +"%Y-%m-%d"
2021-01-17

Print yesterday (1 day ago) date

[[email protected] ~]# date -d "1 day ago"
Sat Jan 16 07:31:22 EST 2021
[[email protected] ~]# date -d "1 day ago" +"%Y-%m-%d"
2021-01-16

Addition and subtraction of time

#Current time
[[email protected] ~]# date +"%Y-%m-%d %H:%M:%S"
2021-01-17 07:38:17
#Time of the next day (tomorrow)
[[email protected] ~]# date -d "+1 day" +"%Y-%m-%d %H:%M:%S"
2021-01-18 07:38:50
#Time of the previous day (yesterday)
[[email protected] ~]# date -d "-1 day" +"%Y-%m-%d %H:%M:%S"
2021-01-16 07:38:56
#Time of the next month (next month)
[[email protected] ~]# date -d "+1 month" +"%Y-%m-%d %H:%M:%S"
2021-02-17 07:39:21
#Time of the previous month (last month)
[[email protected] ~]# date -d "-1 month" +"%Y-%m-%d %H:%M:%S"
2020-12-17 07:39:35
#The next week
[[email protected] ~]# date -d "+1 week" +"%Y-%m-%d %H:%M:%S"
2021-01-24 07:39:52
#Previous time
[[email protected] ~]# date -d "-1 week" +"%Y-%m-%d %H:%M:%S"
2021-01-10 07:39:59
#Time of next year (next year)
[[email protected] ~]# date -d "+1 year" +"%Y-%m-%d %H:%M:%S"
2022-01-17 07:40:11
#Time of the previous year (last year)
[[email protected] ~]# date -d "-1 year" +"%Y-%m-%d %H:%M:%S"
2020-01-17 07:40:19

Set time

date  - s       # Set the current time. Only root permission can set it
date  - s   twenty million two hundred and one thousand one hundred and eleven    # Set to 20201111, which will set the specific time to empty 00:00:00
date  - s   00:00:00    # Set the specific time, and the date will not be changed
#Set all time
date -s "00:00:00 2020-11-11"  
date -s "01:01:01 20201111"    
date -s "2020-11-11 00:00:00"   
date -s "20201111 00:00:00"

Time conversion

[[email protected] ~]# date -d "2021-01-01 1234567890 seconds" +"%Y-%m-%d %H:%m:%S"
2060-02-14 23:02:30
[[email protected] ~]# date -d "2021-01-01" +"%Y-%m-%d %H:%m:%S"
2021-01-01 00:01:00
[[email protected] ~]# date -d "2021-01-01 7890 seconds" +"%Y-%m-%d %H:%m:%S"
2021-01-01 02:01:30

Learn a Linux command every day (47): DF

Learn a Linux command every day (48): Top

Reply to the background of this official account.Learn commandYou can view previous articles to learn Linux system commands.

Recommended Today

Supervisor

Supervisor [note] Supervisor – H view supervisor command help Supervisorctl – H view supervisorctl command help Supervisorctl help view the action command of supervisorctl Supervisorctl help any action to view the use of this action 1. Introduction Supervisor is a process control system. Generally speaking, it can monitor your process. If the process exits abnormally, […]