This Linux command can turn back the clock! You don’t know the hidden usage of date

Time:2021-7-14

Today, I wrote a script for the project. I need to get the time of the previous day. Originally, I got the time of today and then subtracted it. If it’s the 1st, I have to consider the big and small months. It’s very complicated, so I checked the manualdateCommand native support, overjoyed, today we talk about this seemingly insignificantdateOrders.

The article is continuously updated every week. Your “three links” are my greatest affirmation. WeChat can search the public for the first time to read the official account of “backend Technology School” (usually one to two updates earlier than blogs).

Students who use Linux should be familiar with the development of LinuxdateCommand is not strange, often need to click on the command line to get the current time. However, this is only the tip of his ability iceberg.

[[email protected] ~]$ date 
Wednesday, February 12, 2020 19:51:46 CST

Routine operation

Get the time stamp, the number of seconds from 0:00:00 on January 1, 1970 to now

[[email protected] ~]$ date +%s
1581508426

Time stamp restore, restore the number of seconds to the time string

[[email protected] ~]$ date -d "@1581508426"
Wednesday, February 12, 2020 19:53:46 CST

Converts the specified time string to a timestamp

[[email protected] ~]$ date -d '02/22/2222 07:21:22' +%s
7956832882
#Or
[[email protected] ~]$ date -d '2222-02-22 07:21:22' +"%s"
7956832882

Format output time format

[[email protected] ~]$ date "+%Y-%m-%d"
2020-02-12
[[email protected] ~]$ date "+%H:%M:%S"
20:01:53
[[email protected] ~]$ date "+%Y-%m-%d %H:%M:%S"
2020-02-12 20:02:06

Refer to man manual for specific format

Format controls the output format. This format is valid only if the option is global time. They are explained as follows:

       %%% of text

       %A short for the day of the week in the current area (sun.. SAT)

       %A full name of the day of the week in the current area (different lengths) (Sunday.. Sunday)

       %B short for the month of the current region (Jan.. DEC)

       %B full name of the month in the current region (January.. December)

       %C date and time of current area (SAT Nov 04 12:02:33 EST 1989)

       %What is the date of D (in the month) (indicated by two digits) (01.. 31)

       %D date (in month / date / year format) (mm / DD / yy)

       %What is the date of E (in the month) (1.. 31)

       %H is the same as% B

       %H hours (displayed in 24-hour system with two digits) (00.. 23)

       %I hours (displayed in 12 hour system with two digits) (01.. 12)

       %J (day of the year) (in three digits) (001.. 366)

       %K hours (display in 24 hours system, display in zero) (0.. 23)

       %1 hour (displayed in 12 hour system, indicated by zeroing) (1.. 12)

       %Month m (in two digits) (01.. 12)

       %M minutes (in two digits) (00.. 59)

       %N line feed

       %P is the current time am or pm
       
       %R time, displayed in 12 hour system (HH: mm: SS [A / P] m)

       %S from 0:00:00 on January 1, 1970 to the present time (GNU expansion)

       %S seconds (in two digits) (00.. 60)

       %Tab tab in horizontal direction

       %T time, displayed in 24-hour system (HH: mm: SS)

       %U (the week of the year) beginning with Sunday (in two digits) (00.. 53)

       %V (the week of the year) beginning with Monday (in two digits) (01.. 52)

       %W is the day of the week (0.. 6); 0 for Sunday

       %W (the week of the year) beginning with Monday (in two digits) (00.. 53)

       %X displays the current date in (mm / DD / yy) format

       %X displays the current time in the format of% H% m% s

       %The last two digits of year y (00.. 99)

       %Year y (in 4 digits) (1970...)

       %Z display according to the digital time zone specified in RFC-822 (e.g. - 0500) (non-standard extension)

       %Z time zone (for example, EDT) is empty if you cannot decide which time zone it is

The following is the more Sao operation, I used today.

Gets the time of tomorrow relative to the current time

[[email protected] ~]$ date -d next-day
Thursday, February 13, 2020 20:08:35 CST

#You can specify the output format, such as
[[email protected] ~]$ date -d next-day +%Y%m%d
20200213

Gets the time of yesterday relative to the current time

[[email protected] ~]$ date -d last-day
Tuesday, February 11, 2020 20:11:35 CST

#You can also specify the output format, such as
[[email protected] ~]$ date -d last-day +%Y%m%d
20200211

Gets the time of the last month relative to the current time

[[email protected] ~]$ date -d last-month
Sunday, January 12, 2020 20:13:20 CST

#You can also specify the output format, such as
[[email protected] ~]$ date -d last-month +%Y-%m-%d
2020-01-12

Gets the time of the next month relative to the current time

[[email protected] ~]$ date -d next-month
Thursday, March 12, 2020 20:15:44 CST

[[email protected] ~]$ date -d next-month "+%Y-%m-%d %H:%M:%S"
2020-03-12 20:15:38

Gets the time of next year relative to the current time

[[email protected] ~]$ date -d next-year
Friday, February 12, 2021 20:17:21 CST

Gets the time of the previous year relative to the current time

[[email protected] ~]$ date -d last-year
Tuesday, February 12, 2019 20:17:29 CST

more

You can search the official account of WeChat for “back end technology school” to reply to “information”. The article is updated every week. See you next time!