Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of Shell date format

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "Shell date format case analysis", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Shell date format case analysis" bar!

Date / time is a very important value no matter which language it is. For example, when we save a log, it is often a prefix plus the current time, so that the log file name can be unique. In the Shell environment, our command to get time is date, but the format and content of the time and date from date may not be what we want, so we may need to format it.

The common time domains are as follows:

% Y year (for example, 1970, BI, 2018, etc.)% m month (01.12)% d (01.31)% H hour (00.23)% M minute (00.59)% S second (00.59)

Use the date command with no arguments to get the current time and date. The result is usually the time in the CST standard format.

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

To get a specific in 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. You can add the-d option to the above command.

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

Get an hour before the current time

[alvin@VM_0_16_centos ~] $date-d'today-1 hour' +'% Y%m%d%H'2018110414 or [alvin@VM_0_16_centos ~] $date +'% Y-%m-%d% HV% MV% S'-d'- 1 hours'2018-11-0414: 43:38

Gets the day before the specified date. This is actually asking for relative time. For example, the following asks for the date of the day before the National Day:

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

Convert a date to a timestamp

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

Convert the timestamp back to the date

[alvin@VM_0_16_centos] $date-d @ 1541317781Sun 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 Thank you for your reading. This is the content of "instance Analysis of Shell date format". After the study of this article, I believe you have a deeper understanding of the problem of instance analysis of Shell date format, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report