How many formats do you know about Shell date under Linux?

How many formats do you know about Shell date under Linux?

Regardless of the language, date/time is a very important value. For example, when we save logs, we often use a certain prefix plus the current time, so that the log file name can be unique.

In the Shell environment, our command to get the time is date, but the time and date format and content of the date may not be what we want, so we may need to format it for output.

The commonly used time domains are as follows:

% Y year (for example: 1970, 2018, etc.)

% m month (01..12)

% d Day of the month (01..31)

% H hours (00..23)

% M minutes (00..59)

% S seconds (00..59)

Use the date command without parameters to get the current time and date. The time obtained in this way is generally in the CST standard format.

[alvin@VM_0_16_centos ~]$ date
Sat Nov  3 22:01:57 CST 2018

To get a specific format, the command is: date +'format'. Note that this is case sensitive.

[alvin@VM_0_16_centos ~]$ date +'%Y-%m-%d'
2018-11-03
[alvin@VM_0_16_centos ~]$ date +'%Y/%m/%d %H:%M:%S'
2018/11/03 22:08:14

Get yesterday's date. The -d option can be added to the above command.

[alvin@VM_0_16_centos ~]$ date -d 'yesterday' +'%Y/%m/%d %H:%M:%S'
2018/11/02 22:24:31

或者

[alvin@VM_0_16_centos ~]$ date -d 'today -1 day' +'%Y-%m-%d'
2018-11-04

Get the hour before the current time

[alvin@VM_0_16_centos ~]$ date -d 'today -1 hour' +'%Y%m%d%H'
2018110414

或者

[alvin@VM_0_16_centos ~]$ date +'%Y-%m-%d %H:%M:%S' -d '-1 hours'
2018-11-04 14:43:38

Get the day before the specified date. This is actually seeking relative time. For example, the following is the date before the National Day:

[alvin@VM_0_16_centos ~]$ date -d '20181001 -1 day' +'%Y%m%d'
20180930

Convert date to timestamp

[alvin@VM_0_16_centos ~]$ date -d "Nov  4 15:49:41 CST 2018" +%s
1541317781

Convert timestamp back to date

[alvin@VM_0_16_centos ~]$ date -d @1541317781
Sun Nov  4 15:49:41 CST 2018

Convert the timestamp to a date and display it in a specific format

[alvin@VM_0_16_centos ~]$ date -d @1541317781 +'%Y%m%d %H:%M:%S'
20181104 15:49:41

More exciting content, please pay attention to the public number Liang Xu Linux , a public reply to 1024 is available free 5T technical information, including: Linux, C / C ++, Python, raspberry pie, embedded, Java, artificial intelligence , and so on. Reply to the group in the official account and invite you to join the expert cloud technology exchange group.

img

Finally, recently many friends asked me for the Linux learning roadmap , so based on my experience, I spent a month staying up late in my spare time and compiled an e-book. Whether you are in an interview or self-improvement, I believe it will help you! The directory is as follows:

Give it to everyone for free, just ask you to give me a thumbs up!

Ebook | Linux development learning roadmap

I also hope that some friends can join me to make this e-book more perfect!

Gain? I hope the old irons will have a three-strike combo so that more people can read this article

Recommended reading:

Guess you like

Origin blog.csdn.net/yychuyu/article/details/108569144