Use of Linux date command

Use of Linux date command

1. Command Introduction

The date command is used to display the current time or a specified time in a specified format, and can also set the system time. Many Shell scripts need to print time or date in different formats, and perform operations based on time and date. In this case, you can use the date command to complete it. In Unix-like systems, dates are stored as an integer representing the number of seconds that have elapsed since 00:00:00 January 1, 1970 Coordinated Universal Time (UTC), i.e., the Unix timestamp.

2. Command format

date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

Among them, FORMAT is a format control string, which can take the following values:

%% Character%
%a Abbreviation of the day of the week (Sun~Sat)
%A Full name of the day of the week (Sunday~Saturday)
%b Abbreviation of month (Jan~Dec)
%B Full name of the month (January~December)
%c Date and time (Thu 06 Dec 2018 09:43:53 AM CST). Entering only the date command will display the same results.
%C century. Similar to %Y, but does not display the last two digits, such as 20
%d The day of the month (01~31)
%D Date, equivalent to %m/%d/%y, such as 12/06/18
%e The day of the month (1 to 31), single digits are padded with spaces, equivalent to %_d
%F Date, equivalent to %Y-%m-%d, such as 2018-12-06
%g The last two digits of the year (yy). For example, 2018 will output 18, which is equivalent to %y
%G Year (yyyy)
%h Abbreviation of month (Jan~Dec), equivalent to %b
%H Hour, 24-hour system (00~23)
%I Hour, 12-hour format (01~12)
%j The day of the year (001~366)
%k Hour, 24-hour system (0~23). Single digit fills the space, equivalent to %_H
%l Hour, 12-hour format (1 to 12). Single digit fills the space, equivalent to %_I
%m Month (01~12)
%M Minute (00~59)
%n newline
%N nanoseconds (000000000..999999999)
%p displays AM or PM
%P displays am or pm
%r Display time in 12-hour format (hh:mm:ss %p)
%R displays hours and minutes, 24-hour format, equivalent to %H:%M
%s The number of seconds from 00:00:00 on January 1, 1970 to the present. %S Displays seconds (00~59)
%t Tab character %T Displays time in 24-hour format (hh:mm:ss), equivalent to %H:%M:%S
%u Day of the week (1..7). 1 means Monday %U Week number of the year, with Sunday as the first day of the week (00..53)
%V Week number of the year, with Monday as the first day of the week (01..53)
%w The day of the week (0-6), 0 represents Sunday %W The week of the year, Monday is the first day of the week (00..53)
%x Date (mm/dd/yyyy), such as 12/06/2018
%X time, equivalent to %H:%M:%S
%y The last two digits of the year (18 for 2018)
%Y Year (yyyy)
%z displays the time zone in +hhmm format (e.g. +0800)
%:z displays the time zone in +hh:mm format (e.g. +08:00)
%::z displays the time zone in +hh:mm:ss format (e.g. +08:00:00)
%Z abbreviation shows the time zone name, such as CST (China Standard Time)
%h,%b Abbreviation of month (Jan~Dec)
Note on filling characters: By default, the date command fills numeric fields with 0s. The following fill character control characters can be used after %:
- (hyphen): no padding _ (underscore): padding with spaces 0 (zero) padding with 0 ^ Output in uppercase whenever possible # Output in reverse case whenever possible

3. Command Options

-d, --date=STRING: Display the time specified by STRING instead of the current timestamp;
-f, --file=DATEFILE: Display the time of each line in the DATEFILE file;
-I[TIMESPEC], --iso-8601[=TIMESPEC]: Display the time in ISO 8601 canonical format with the specified precision [TIMESPEC]. The default value of TIMESPEC is "date", and it can also be 'hours', 'minutes', 'seconds', or 'ns';
-r, --reference=FILE: Display the last modification time of the file -R, --rfc-2822: Display the time in the format specified in RFC-2822, for example: Wed, 05 Dec 2018 22:10:34 +0800
--rfc-3339=TIMESPEC: Display time in the format specified by RFC 3339. The precision can be specified by TIMESPEC. TIMESPEC can take values ​​'date', 'seconds', or 'ns'. For example: 2018-12-05 22:09:59.230994842+08:00
-s, --set=STRING: Set the system time to the time specified by STRING -u, --utc, --universal: Display or set the time format to Coordinated Universal Time (UTC) --help: Display help information for the date command --version: Display version information for the date command

4. Common Examples

(1) Get the Unix timestamp.

date +%s
1544067345

(2) Convert the Unix timestamp to readable time.

date -d @1483525407
Wed Jan 4 18:23:27 CST 2017

date -d @1483525407 +"%F %T"
2017-01-04 18:23:27

Note: -d must be followed by a date in a legal format, so the timestamp needs to be distinguished by an @ character.

(3) Format and output the current time.

date +"%Y-%m-%d %H:%M:%S"
2018-12-06 10:57:33

# or date + "%F %T"

(4) Time addition and subtraction operations.

date +"%Y-%m-%d %H:%M:%S" //Display the current timedate -d "+1 day" +"%Y-%m-%d %H:%M:%S" //Display the previous daydate -d "-1 day" +"%Y-%m-%d %H:%M:%S" //Display the next daydate -d "-1 month" +"%Y-%m-%d %H:%M:%S" //Display the previous monthdate -d "+1 month" +"%Y-%m-%d %H:%M:%S" //Display the next monthdate -d "-1 year" +"%Y-%m-%d %H:%M:%S" //Display the previous yeardate -d "+1 year" +"%Y-%m-%d %H:%M:%S" //Display the next year

(5) Common format conversion.

date -d "2009-12-12" +"%Y/%m/%d %H:%M:%S"
2009/12/12 00:00:00

(6) Set the system time.

date -s "2016-11-11 00:00:00"
Fri Nov 11 00:00:00 CST 2016

date
Fri Nov 11 00:00:05 CST 2016

The above is the detailed content of the use of Linux date command. For more information about Linux date command, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Use of Linux gzip command
  • Usage of Linux userdel command
  • How to run Linux commands in the background
  • Use of Linux stat command
  • Use of Linux ls command
  • Use of Linux ln command
  • Linux cut command explained
  • Use of Linux bzip2 command

<<:  Analysis of the principles of Mysql dirty page flush and shrinking table space

>>:  MySQL chooses the right storage engine

Recommend

Vue3 implements CSS infinite seamless scrolling effect

This article example shares the specific code of ...

MySQL partition table is classified by month

Table of contents Create a table View the databas...

MySQL 8.0.15 compressed version installation graphic tutorial

This article shares the installation method of My...

Detailed explanation of the usage of the ESCAPE keyword in MySQL

MySQL escape Escape means the original semantics ...

Teach you how to build the vue3.0 project architecture step by step

Table of contents Preface: 1. Create a project wi...

Detailed View of Hidden Columns in MySQL

Table of contents 1. Primary key exists 2. No pri...

An article tells you how to implement Vue front-end paging and back-end paging

Table of contents 1: Front-end handwritten paging...

Solution to many line breaks and carriage returns in MySQL data

Table of contents Find the problem 1. How to remo...

JavaScript BOM Explained

Table of contents 1. BOM Introduction 1. JavaScri...

7 useful new TypeScript features

Table of contents 1. Optional Chaining 2. Null va...

MySQL sorting using index scan

Table of contents Install sakila Index Scan Sort ...

How to view mysql binlog (binary log)

For example, when you create a new table or updat...