Learn a little shell every day: the usage of date of time function in linux

1. Format of date function in linux

First, the format of the date function is as follows:

用法:date [选项]... [+格式]
 或:date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

The format is explained as follows:

symbol Explanation
%% % Of a text
%a The abbreviation of the week name of the current locale (for example: day, representing Sunday)
%A The full week name of the current locale (e.g. Sunday)
%b The abbreviation of the month name of the current locale (e.g. one, representing January)
%B The full month name of the current locale (e.g. January)
%c The date and time of the current locale (eg: 23:05:25, Thursday, March 3, 2005)
%C Century; such as %Y, usually omitting the last two digits of the current year (for example: 20)
%d Month by month (e.g. 01)
%D Date in month; equal to %m/%d/%y
%e Month by month, add space, equal to %_d
%F Full date format, equivalent to %Y-%m-%d
%g The last two digits of the year in ISO-8601 format (see %G)
%G ISO-8601 format year (see %V), generally only used in combination with %V
%h Equal to %b
%H Hour (00-23)
%I Hour (00-12)
%j Yearly date (001-366)
%k hour, space padded ( 0…23); same as %_H
%l hour, space padded ( 1…12); same as %_I
%m month (01…12)
%M minute (00…59)
%n Wrap
%N Nanosecond (000000000-999999999)
%p "AM" or "PM" under the current locale, the output is empty when unknown
%P Similar to %p, but output lowercase letters
%r The 12-hour clock time under the current locale (eg: 11:11:04 PM)
%R Hours and minutes of 24 hours, equivalent to %H:%M
%s The number of seconds that have passed since UTC 1970-01-01 00:00:00
%S Seconds (00-60)
%t Tab output Tab
%T Time, equal to %H:%M:%S
% u Week, 1 for Monday
% U The first few weeks of the year, with Sunday as the first day of the week (00-53)
% V The week of the year under the ISO-8601 format specification, with Monday as the first day of the week (01-53)
%w The day of the week (0-6), 0 means Monday
%W The first few weeks of the year, with Monday as the first day of the week (00-53)
%x The date description under the current locale (eg: 12/31/99)
%X Time description under current locale (eg: 23:13:48)
%and The last two digits of the year (00-99)
%AND years
%WITH Alphabetical time zone abbreviation (for example, EDT)

2. The specific usage of date function

2.1, get the date in the corresponding format

The output date format is yyyy-MM-dd HH:mm:ssthe time, taking the current date and time as an example:

method one:

[root@hadoop-master ~]# date -d today +"%Y-%m-%d %T"
2020-09-16 19:45:39

Way two:

[root@hadoop-master ~]# date +"%Y-%m-%d %T"
2020-09-16 19:48:39

Way three:

[root@hadoop-master ~]# date +"%Y-%m-%d %H:%M:%S"
2020-09-16 19:51:42

Give an example of other data formats, for example, the output format is yyyMMddHHmmssformat

[root@hadoop-master ~]# date +"%Y%m%d%H%M%S"
20200916195408

2.2, get the date of the interval

2.2.1, get today's date

method one:

[root@hadoop-master ~]# date +%Y%m%d
20200916

Way two:

[root@hadoop-master ~]# date +%F
2020-09-16

Way three:

[root@hadoop-master ~]# echo $(date +%Y%m%d)
20200916

2.2.2, get yesterday's date

method one:

[root@hadoop-master ~]# date -d yesterday +%Y%m%d
20200915

Way two:

[root@hadoop-master ~]# date -d -1day +%Y%m%d
20200915

Way three:

[root@hadoop-master ~]# date -d "-1 day" +%Y%m%d
20200915

Way four:

[root@hadoop-master ~]# date +%Y%m%d --date="-1 day"
20200915

Way Five:

[root@hadoop-master ~]# date +%Y%m%d --date="1 day ago"
20200915

Based on the above expression, we infer that the date 17 days ago is:

[root@hadoop-master ~]# date +%Y%m%d --date=-17day
20200830

In the same way, it can be inferred that the last hour is:

[root@hadoop-master ~]# date +"%Y-%m-%d %H:%M:%S"  --date=-1hour
2020-09-16 19:15:00

Last minute:

[root@hadoop-master ~]# date +"%Y-%m-%d %H:%M:%S"  --date=-1minute
2020-09-16 20:14:34

In the same way, you can get the last year, last month, last day, last hour, last minute, and last second. No more expansion.

2.2.3, get tomorrow's date

Based on the above, we can easily infer how the date will be calculated tomorrow:
Method 1:

[root@hadoop-master ~]# date +%Y%m%d --date=tomorrow
20200917

Way two:

[root@hadoop-master ~]# date +%Y%m%d --date=+1day
20200917

Way three:

[root@hadoop-master ~]# date +%Y%m%d --date="+1 day"
20200917

Similarly, it can be inferred that after 17 days:

[root@hadoop-master ~]# date +%Y%m%d --date="+17 day"
20201003

2.3, get the current timestamp

Note the number of seconds that have passed since UTC time 1970-01-01 00:00:00. If you are using a more accurate time stamp, you cannot use this

[root@hadoop-master ~]# date +%s
1600258838

The following is the number of nanoseconds to get the current time, accurate to one hundred millionth of a second, this is the value after the second

[root@hadoop-master ~]# date +%N
713781544

Get the current time in milliseconds

[root@hadoop-master ~]# echo $((`date '+%s'`*1000+`date '+%N'`/1000000))
1600259292777

However, there is a defect in this one that will report an error:

[root@hadoop-master ~]# echo $((`date '+%s'`*1000+`date '+%N'`/1000000))
-bash: 1600259263*1000+097437620: 数值太大不可为算数进制的基 (错误符号是 "097437620"

2.4. Convert the specified time string into a date format

Convert string to timestamp

[root@hadoop-master ~]# date +%s --date="2020-09-16"
1600185600

Convert timestamp to time

[root@hadoop-master ~]#   date -d @1600185600  "+%Y-%m-%d"
2020-09-16

Guess you like

Origin blog.csdn.net/u011047968/article/details/108628765