This is the 10th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

A lifelong learner, practitioner, and sharer committed to the path of technology, an original blogger who is busy and sometimes lazy, and a teenager who is occasionally boring and sometimes humorous.

Welcome to search “Jge’s IT Journey” on wechat!

Play with date syntax in Linux

When you first learn Linux, you may run a date command to check the date, but the date format and content may not be what we want, so we need to further output it to achieve the format we want. Today, we will take you to play with the basic syntax of the date in the Linux environment, you may wish to operate on the machine, the effect will be better. The syntax for each query in the following terminal box is slightly different.

Common time/date field

When the date command is used, you can obtain the current system time without any parameter. It is the time in CST standard format.

[root@localhost ~]# date 2019 Apr 08 Mon 09:43:37CstCopy the code

In addition to the above parameters, you can also use date +%r to get the current time;

[root@localhost ~]# date +%r 01/22/06Copy the code

To obtain a specific format, run the following command: date + ‘format’. Note the case of the time domain.

[root@localhost ~]# date + '%Y-%m-%d' [root@localhost ~]# date + '%Y-%m-%d' [root@localhost ~]# date + '%Y-%m-%d [root@localhost ~]# date +'%Y-%m-%d' 2019-04-08 [root@localhost ~]# date +'%Y/%m/%d %H:% m :%S' Time: 2019/04/08 09:47:44Copy the code

Get the date (year, month, day, hour, minute, second) of yesterday, the day before yesterday, and the days before that. You can add the parameter -d.

[root@localhost ~]# date -d 'yesterday' 2019年 04月 07日 09:48:41 CST [root@localhost ~]# date -d last-day +%Y%m%d 20190407 [root@localhost ~]# date -d 'yesterday' +'%Y/%m/%d %H:%M:%S' 2019/04/07 10:02:26 [root@localhost ~]# date -d last-day +'%Y%m%d %H:%M:%S' 20190407 10:43:04 [root@localhost ~]# date -d 'today -1 day' +'%Y-%m-%d' 2019-04-07 [root@localhost ~]# date -d 'yesterday -1 day' +'%Y-%m-%d' 2019-04-06 [root@localhost ~]# date -d 'yesterday -1 day' Y + '% % m - H: % d % % m: % S' # the timing of the current date two days before the 2019-04-06 11:09:35 / root @ localhost ~ # date - d "6 days line" + '% % Y - m - H: % d % % m: % S' # Time point 6 days before the current date 2019-04-02 11:13:23Copy the code

Get the point in time lookup syntax for tomorrow, the day after tomorrow, and a few days after the current date;

[root@localhost ~]# date -d 'tomorrow' 2019 apr 09 Tuesday 10:51:21cst [root@localhost ~]# date -d tomorrow +%Y%m%d 20190409 [root@localhost ~]# date -d 'tomorrow' +'%Y%m%d %H:%M:%S' 20190409 10:53:56 [root@localhost ~]# date -d tomorrow +'%Y%m%d %H:%M:%S' 20190409 10:54:57 [root@localhost ~]# date -d tomorrow +'%Y-%m-%d %H:%M:%S' 2019-04-09 10:56:09 [root@localhost ~]# date -d tomorrow +'%Y-%m-%d' 2019-04-09 [root@localhost ~]# date -d next-day +%Y%m%d 20190409 [root@localhost ~]# date -d next-day +'%Y-%m-%d' 2019-04-09 [root@localhost ~]# date -d 'next-day' +'%Y%m%d %H:%M:%S' 20190409 10:54:06 [root@localhost ~]# date -d next-day +'%Y%m%d %H:%M:%S' 20190409 10:55:12 [root@localhost ~]# date -d next-day +'%Y-%m-%d %H:%M:%S' 2019-04-09 10:55:59 [root@localhost ~]# date -d 'tomorrow -l' +'%Y-%m-%d' [root@localhost ~]# date -d 'tomorrow -l day' +'%Y-%m-%d' # date -d 'tomorrow -l day' +'%Y-%m-%d' # date -d 'tomorrow -l day' +'%Y-%m-%d ~] # date - d - 6 days "line" + '% % Y - m - H: % d % % m: % S' # 6 days after the date the current point in time the 2019-04-14 11:14:51Copy the code

Get syntax for the previous hour of the current time;

[root@localhost ~]# date -d 'today -1 hour' +'%Y%m%d%H'
2019040810
[root@localhost ~]# date +'%Y-%m-%d %H:%M:%S' -d '-1 hours'
2019-04-08 10:22:01
Copy the code

Gets the date syntax for the day before and the day after the specified date;

[root@localhost ~]# date -d '20190101 -1 day' +'%Y%m%d' 20181231 [root@localhost ~]# date -d '20190101 yesterday' +'%Y%m%d' 20181231 [root@localhost ~]# date -d '20190101 yesterday -5 day' +'%Y%m%d' 20181226 [root@localhost ~]# date -d '20190101 yesterday' +'%Y%m%d %H:%M:%S' 20181231 00:00:00 [root@localhost ~]# date -d '20190101 yesterday -5 day' +'%Y%m%d %H:%M:%S' 20181226 00:00:00 [root@localhost ~]# date -d '20190101 1 day' +'%Y%m%d' 20190102 [root@localhost ~]#  date -d '20190101 -l day' +'%Y%m%d' 20190102 [root@localhost ~]# date -d '20190101 tomorrow' +'%Y%m%d' 20190102 [root@localhost ~]# date -d '20190101 tomorrow 6 day' +'%Y%m%d' 20190108 [root@localhost ~]# date -d '20190101 tomorrow'  +'%Y%m%d %H:%M:%S' 20190102 00:00:00 [root@localhost ~]# date -d '20190101 tomorrow 6 day' +'%Y%m%d %H:%M:%S' 20190108 00:00:00Copy the code

Get the date and time point syntax from one week ago;

[root@localhost ~]# date -d "1 week ago" +%Y%m%d
20190401
[root@localhost ~]# date -d "1 week ago" +'%Y%m%d %H:%M:%S'
20190401 12:56:04
Copy the code

Get the year and month of last and next month;

[root@localhost ~]# date -d last-month +%Y%m
201903
[root@localhost ~]# date -d next-month +%Y%m
201905
Copy the code

Get the year of the previous year and the year of the next year;

[root@localhost ~]# date -d last-year +%Y
2018
[root@localhost ~]# date -d next-year +%Y
2020
Copy the code

Get the date and time point syntax from one year ago;

[root@localhost ~]# date -d "1 year ago" +%Y%m%d
20180408
[root@localhost ~]# date -d "1 year ago" +'%Y%m%d %H:%M:%S'
20180408 12:58:10
Copy the code

Gets the day of the year that the current date is;

[root@localhost ~]# date "+%j"
098
Copy the code

Get the current month/day/year;

[root@localhost ~]# date +%D
04/08/19
Copy the code

You can run the cal-y command to obtain the calendar of each month of the year.

You can run the command cal-3 to obtain the calendar of the current month and the calendar of the next two months. If no parameter is specified, the calendar of the current month is displayed.

Convert the date to a timestamp

[root@localhost ~]# date -d "Mar 6 23:59:59CST 2019" +%s
1551887999
Copy the code

Convert a timestamp to a date

[root@localhost ~]# date -d @1551887999 March 06, 2019 Wednesday 23:59:59CSTCopy the code

Converts a timestamp to a date and outputs it in a specific format.

[root@localhost ~]# date -d @1551887999 +'%Y%m%d %H:%M:%S'
20190306 23:59:59
Copy the code

Recommended reading

99% of Linux operation and maintenance engineers must master the command and use

Common commands of the Oracle database in Linux

Common commands of the vi/vim editor in Linux

Install and manage programs in Linux (basic process of package encapsulation, RPM command, source code compilation and installation)

Manage accounts and rights in Linux

Linux disk and file system management

Process and scheduled task management in Linux

Analyze and rectify system faults in Linux

Common KVM commands in Linux

In this paper, to the end.


Original is not easy, if you think this article is a little useful to you, please give me a like, comment or forward for this article, because this will be my power to output more quality articles, thanks!

By the way, dig friends remember to give me a free attention yo! In case you get lost and you can’t find me next time.

See you next time!