Linux basic commands: date Linux date command Detailed

1, the command: date

2, Function: date or can be used to display the date and time setting system.

3, command parameters

-d <string>: date and time display string referred to. Before and after the string must be enclosed in double quotes; 
-s <string>: to set the date and time according to the character string. Before and after the string must be enclosed in double quotes; 
-u: display GMT; 
--help: online help; 
--version: show the version information.

4, the date format string list

Copy the code
% H hours (represented 00-23). 
% I h (expressed in 01-12). 
H% K (expressed in 0-23). 
h% l (expressed 0-12). 
% M min (00-59 represented). 
% P AM or PM. 
% r time (minutes and seconds when the content, expressed in hours 12 hours AM / PM). 
% s total number of seconds. Starting time is 1970-01-01 00:00:00 UTC. 
% S seconds (expressed in local usage). 
% T time (minutes and seconds when containing hours in 24-hour format). 
% X time (expressed in local usage). 
% Z downtown. 
Abbreviations% a week. 
% A week's full name. 
% b abbreviated English name of the month. 
% B full English name of the month. 
% c date and time. Only instruction input date also show the same results. 
Date% d (expressed in 01-31). 
% D date (including date). 
% j day of the year in. 
Month% m (expressed in 01-12). 
Weeks of the year in% U. 
% w the number of days of the week, 0 for Sunday, 1 for Monday, and so on different terms. 
Date% x (expressed in local usage). 
% y Year (00-99 to be represented). 
% Y year (expressed in four digits). 
% n in the display, insert a new row.
% t in the display, insert tab. 
MM Month (necessary) 
DD date (necessary) 
HH h (necessary) 
mm minute (necessary) 
SS seconds (optional)
Copy the code

5, an example

Formatted output:

date +"%Y-%m-%d" 
2015-12-07

Output yesterday Date:

date -d "1 day ago" +"%Y-%m-%d"
2015-11-19

After 2 seconds Output:

date -d "2 second" +"%Y-%m-%d %H:%M.%S"
2015-11-20 14:21.31

Legendary 1,234,567,890 seconds:

date -d "1970-01-01 1234567890 seconds" +"%Y-%m-%d %H:%m:%S"
2009-02-13 23:02:30

Ordinary transfer format:

date -d "2009-12-12" +"%Y/%m/%d %H:%M.%S"
2009/12/12 00:00.00

apache format conversion:

date -d "Dec 5, 2009 12:00:37 AM" +"%Y-%m-%d %H:%M.%S"
2009-12-05 00:00.37

Format conversion after time:

date -d "Dec 5, 2009 12:00:37 AM 2 year ago" +"%Y-%m-%d %H:%M.%S"
2007-12-05 00:00.37

Subtraction operation:

Copy the code
date +% Y% m% d # day before the date display 
date -d "+1 day" +% Y% m% d # previous day of the date display 
date -d "-1 day" +% Y% m% d after the day's date # display 
date -d "-1 month" +% Y% m% d # date Jan. displayed on the 
date -d "+1 month" +% Y% m% d # displays the date of the next month 
date -d "-1 year" +% Y% m% d # display year before the date of 
date -d "+1 year" +% Y% m% d # display date year
Copy the code

set time:

Copy the code
date -s # set the current time, only root privileges to set up, the other can only view the 
date -s 20120523 # set to 20,120,523, which will set the specific time to empty 00:00:00 
DATE -s 01:01:01 # Set the specific time, date change will not do to 
date -s "01:01:01 2012-05-23" # this sets all-time 
date -s "01:01:01 20120523" # this sets all-time 
date -s "2012-05-23 01:01:01" # this sets all-time 
date -s "20120523 01:01:01" # this sets all the time
Copy the code

Checking a set of time it takes to command:

Copy the code
#!/bin/bash 
start=$(date +%s) 
nmap man.linuxde.net &> /dev/null 
end=$(date +%s) 
difference=$(( end - start )) 
echo $difference seconds.
Copy the code
Disclaimer: This article is only author notes, do not reprint!
 
 

Linux date command Detailed

 

1, display time

date command to display the date in the specified format, just type the date in a default format to display the current time. as follows:

To display the date in a specified format, can be used "+" character string at the beginning of the format specified in detail in the following format:

% n: the next line
% t: tabbing
% H: hour (00-23)
% the I: h (01-12)
% K: hour (0-23)
% L: hour (1-12)
% M: min (00-59)
% P: AM local display or the PM
% R & lt: direct display time (12-hour clock format HH: mm: SS [the AP] M)
% S: from at 00:00 on January 1st 1970: 00 UTC seconds to date
% S: seconds (00-60)
% T: direct display time (24 hour)
% X-: equivalent H%:% M:% S
% the Z: display area
% a: week few (the Sun-Sat)
% A: week (the Sunday-Saturday)
% B: month (Jan-On Dec)
% B: month (January-December)
% C: direct display date and time
% d: day (01-31 )
% D: direct display date (mm / dd / YY)
% H: with B%
% J: the first day of the year (001-366)
% m: month (01-12)
% the U-: year the first few weeks (00-53) (in the first day of the week Sunday case)
% W: the day of the week (0-6)
% W: week number (00-53) (in the first day of the week Monday to case) the year
% x: direct display date (mm / dd / YY)
% Y: the last two digits of the year ( 00.99)
% the Y-: full year (0000-9999)

Do not have to remember all the above format, you only need to master a few common. E.g. expressed in% Y,% m is the month,% d represents day,% H represents hours,% M indicates min,% S represents seconds,% s represents from 1 January 1970 00:00:00 UTC date the number of seconds, the equivalent time function,% w represents the day of the week.

Examples below:

If you want to display the current time is not time, but the elapsed time operation, you can use the -d option. Three years ago, for example, display the time:

I was very hurt to see this command, the last time can not go back, only to see the command. Show time after three months:

Show time after ten days:

Sometimes the need to obtain the current time from at 0:00 on 1970 experienced the number of seconds 0 seconds, stored in a variable:

 

2, set the time

You can set the system time with the -s option:

A variety of ways, either you choose:

You have to believe in intelligent enough date of.

 

 


 

 

With more than a place:

Use  the date command  to get the current date, and to create a new folder based on this date.

 

 
 
 

1, the command: date

2, Function: date or can be used to display the date and time setting system.

3, command parameters

-d <string>: date and time display string referred to. Before and after the string must be enclosed in double quotes; 
-s <string>: to set the date and time according to the character string. Before and after the string must be enclosed in double quotes; 
-u: display GMT; 
--help: online help; 
--version: show the version information.

4, the date format string list

Copy the code
% H hours (represented 00-23). 
% I h (expressed in 01-12). 
H% K (expressed in 0-23). 
h% l (expressed 0-12). 
% M min (00-59 represented). 
% P AM or PM. 
% r time (minutes and seconds when the content, expressed in hours 12 hours AM / PM). 
% s total number of seconds. Starting time is 1970-01-01 00:00:00 UTC. 
% S seconds (expressed in local usage). 
% T time (minutes and seconds when containing hours in 24-hour format). 
% X time (expressed in local usage). 
% Z downtown. 
Abbreviations% a week. 
% A week's full name. 
% b abbreviated English name of the month. 
% B full English name of the month. 
% c date and time. Only instruction input date also show the same results. 
Date% d (expressed in 01-31). 
% D date (including date). 
% j day of the year in. 
Month% m (expressed in 01-12). 
Weeks of the year in% U. 
% w the number of days of the week, 0 for Sunday, 1 for Monday, and so on different terms. 
Date% x (expressed in local usage). 
% y Year (00-99 to be represented). 
% Y year (expressed in four digits). 
% n in the display, insert a new row.
% t in the display, insert tab. 
MM Month (necessary) 
DD date (necessary) 
HH h (necessary) 
mm minute (necessary) 
SS seconds (optional)
Copy the code

5, an example

Formatted output:

date +"%Y-%m-%d" 
2015-12-07

Output yesterday Date:

date -d "1 day ago" +"%Y-%m-%d"
2015-11-19

After 2 seconds Output:

date -d "2 second" +"%Y-%m-%d %H:%M.%S"
2015-11-20 14:21.31

Legendary 1,234,567,890 seconds:

date -d "1970-01-01 1234567890 seconds" +"%Y-%m-%d %H:%m:%S"
2009-02-13 23:02:30

Ordinary transfer format:

date -d "2009-12-12" +"%Y/%m/%d %H:%M.%S"
2009/12/12 00:00.00

apache format conversion:

date -d "Dec 5, 2009 12:00:37 AM" +"%Y-%m-%d %H:%M.%S"
2009-12-05 00:00.37

Format conversion after time:

date -d "Dec 5, 2009 12:00:37 AM 2 year ago" +"%Y-%m-%d %H:%M.%S"
2007-12-05 00:00.37

加减操作:

Copy the code
date +%Y%m%d               #显示前天年月日 
date -d "+1 day" +%Y%m%d   #显示前一天的日期 
date -d "-1 day" +%Y%m%d   #显示后一天的日期 
date -d "-1 month" +%Y%m%d #显示上一月的日期 
date -d "+1 month" +%Y%m%d #显示下一月的日期 
date -d "-1 year" +%Y%m%d  #显示前一年的日期 
date -d "+1 year" +%Y%m%d  #显示下一年的日期
Copy the code

设定时间:

Copy the code
date -s          #设置当前时间,只有root权限才能设置,其他只能查看 
date -s 20120523 #设置成20120523,这样会把具体时间设置成空00:00:00 
date -s 01:01:01 #设置具体时间,不会对日期做更改 
date -s "01:01:01 2012-05-23" #这样可以设置全部时间 
date -s "01:01:01 20120523"   #这样可以设置全部时间 
date -s "2012-05-23 01:01:01" #这样可以设置全部时间 
date -s "20120523 01:01:01"   #这样可以设置全部时间
Copy the code

检查一组命令花费的时间:

Copy the code
#!/bin/bash 
start=$(date +%s) 
nmap man.linuxde.net &> /dev/null 
end=$(date +%s) 
difference=$(( end - start )) 
echo $difference seconds.
Copy the code
声明:本文仅为作者个人笔记,请勿转载!
 
 

Linux date命令详解

 

1、显示时间

date命令可以按照指定格式显示日期,只键入date则以默认格式显示当前时间。如下:

如果需要以指定的格式显示日期,可以使用“+”开头的字符串指定其格式,详细格式如下:

%n : 下一行
%t : 跳格
%H : 小时(00-23)
%I : 小时(01-12)
%k : 小时(0-23)
%l : 小时(1-12)
%M : 分钟(00-59)
%p : 显示本地 AM 或 PM
%r : 直接显示时间 (12 小时制,格式为 hh:mm:ss [AP]M)
%s : 从 1970 年 1 月 1 日 00:00:00 UTC 到目前为止的秒数
%S : 秒(00-60)
%T : 直接显示时间 (24 小时制)
%X : 相当于 %H:%M:%S
%Z : 显示时区
%a : 星期几 (Sun-Sat)
%A : 星期几 (Sunday-Saturday)
%b : 月份 (Jan-Dec)
%B : 月份 (January-December)
%c : 直接显示日期与时间
%d : 日 (01-31)
%D : 直接显示日期 (mm/dd/yy)
%h : 同 %b
%j : 一年中的第几天 (001-366)
%m : 月份 (01-12)
%U : 一年中的第几周 (00-53) (以 Sunday 为一周的第一天的情形)
%w : 一周中的第几天 (0-6)
%W : 一年中的第几周 (00-53) (以 Monday 为一周的第一天的情形)
%x : 直接显示日期 (mm/dd/yy)
%y : 年份的最后两位数字 (00.99)
%Y : 完整年份 (0000-9999)

上述格式不必全都记住,只需要掌握几个常用的即可。例如%Y表示年,%m表示月,%d表示日,%H表示小时,%M表示分钟,%S表示秒,%s表示从 1970 年 1 月 1 日 00:00:00 UTC 到目前为止的秒数,相当于time函数,%w表示一周中的第几天。

实例见下:

如果要显示的时间不是当前时间,而是经过运算的时间,则可以用-d选项。例如显示三年前的时间:

看到这个命令我很受伤,过去的时间再也回不去了,只能用命令看看。显示三个月后的时间:

显示十天后的时间:

有时候需要获取当前时间距离1970年0时0分0秒所经历的秒数,保存在变量中:

 

2、设置时间

用 -s选项可以设置系统时间:

方式多种多样,任你选:

你要足够相信date的智能。

 

 


 

 

用得比较多的地方:

利用 date命令 获取当前日期,并根据这个日期来创建新文件夹。

 

1、命令:date

2、命令功能:date 可以用来显示或设定系统的日期与时间。

3、命令参数

-d<字符串>:显示字符串所指的日期与时间。字符串前后必须加上双引号; 
-s<字符串>:根据字符串来设置日期与时间。字符串前后必须加上双引号; 
-u:显示GMT; 
--help:在线帮助; 
--version:显示版本信息。

4、日期格式字符串列表

Copy the code
%H 小时(以00-23来表示)。 
%I 小时(以01-12来表示)。 
%K 小时(以0-23来表示)。 
%l 小时(以0-12来表示)。 
%M 分钟(以00-59来表示)。 
%P AM或PM。 
%r 时间(含时分秒,小时以12小时AM/PM来表示)。 
%s 总秒数。起算时间为1970-01-01 00:00:00 UTC。 
%S 秒(以本地的惯用法来表示)。 
%T 时间(含时分秒,小时以24小时制来表示)。 
%X 时间(以本地的惯用法来表示)。 
%Z 市区。 
%a 星期的缩写。 
%A 星期的完整名称。 
%b 月份英文名的缩写。 
%B 月份的完整英文名称。 
%c 日期与时间。只输入date指令也会显示同样的结果。 
%d 日期(以01-31来表示)。 
%D 日期(含年月日)。 
%j 该年中的第几天。 
%m 月份(以01-12来表示)。 
%U 该年中的周数。 
%w 该周的天数,0代表周日,1代表周一,异词类推。 
%x 日期(以本地的惯用法来表示)。 
%y 年份(以00-99来表示)。 
%Y 年份(以四位数来表示)。 
%n 在显示时,插入新的一行。 
%t 在显示时,插入tab。 
MM 月份(必要) 
DD 日期(必要) 
hh 小时(必要) 
mm 分钟(必要)
ss 秒(选择性) 
Copy the code

5、实例

格式化输出:

date +"%Y-%m-%d" 
2015-12-07

输出昨天日期:

date -d "1 day ago" +"%Y-%m-%d"
2015-11-19

2秒后输出:

date -d "2 second" +"%Y-%m-%d %H:%M.%S"
2015-11-20 14:21.31

传说中的 1234567890 秒:

date -d "1970-01-01 1234567890 seconds" +"%Y-%m-%d %H:%m:%S"
2009-02-13 23:02:30

普通转格式:

date -d "2009-12-12" +"%Y/%m/%d %H:%M.%S"
2009/12/12 00:00.00

apache格式转换:

date -d "Dec 5, 2009 12:00:37 AM" +"%Y-%m-%d %H:%M.%S"
2009-12-05 00:00.37

格式转换后时间:

date -d "Dec 5, 2009 12:00:37 AM 2 year ago" +"%Y-%m-%d %H:%M.%S"
2007-12-05 00:00.37

加减操作:

Copy the code
date +%Y%m%d               #显示前天年月日 
date -d "+1 day" +%Y%m%d   #显示前一天的日期 
date -d "-1 day" +%Y%m%d   #显示后一天的日期 
date -d "-1 month" +%Y%m%d #显示上一月的日期 
date -d "+1 month" +%Y%m%d #显示下一月的日期 
date -d "-1 year" +%Y%m%d  #显示前一年的日期 
date -d "+1 year" +%Y%m%d  #显示下一年的日期
Copy the code

设定时间:

Copy the code
date -s          #设置当前时间,只有root权限才能设置,其他只能查看 
date -s 20120523 #设置成20120523,这样会把具体时间设置成空00:00:00 
date -s 01:01:01 #设置具体时间,不会对日期做更改 
date -s "01:01:01 2012-05-23" #这样可以设置全部时间 
date -s "01:01:01 20120523"   #这样可以设置全部时间 
date -s "2012-05-23 01:01:01" #这样可以设置全部时间 
date -s "20120523 01:01:01"   #这样可以设置全部时间
Copy the code

检查一组命令花费的时间:

Copy the code
#!/bin/bash 
start=$(date +%s) 
nmap man.linuxde.net &> /dev/null 
end=$(date +%s) 
difference=$(( end - start )) 
echo $difference seconds.
Copy the code

1、显示时间

date命令可以按照指定格式显示日期,只键入date则以默认格式显示当前时间。如下:

如果需要以指定的格式显示日期,可以使用“+”开头的字符串指定其格式,详细格式如下:

%n : 下一行
%t : 跳格
%H : 小时(00-23)
%I : 小时(01-12)
%k : 小时(0-23)
%l : 小时(1-12)
%M : 分钟(00-59)
%p : 显示本地 AM 或 PM
%r : 直接显示时间 (12 小时制,格式为 hh:mm:ss [AP]M)
%s : 从 1970 年 1 月 1 日 00:00:00 UTC 到目前为止的秒数
%S : 秒(00-60)
%T : 直接显示时间 (24 小时制)
%X : 相当于 %H:%M:%S
%Z : 显示时区
%a : 星期几 (Sun-Sat)
%A : 星期几 (Sunday-Saturday)
%b : 月份 (Jan-Dec)
%B : 月份 (January-December)
%c : 直接显示日期与时间
%d : 日 (01-31)
%D : 直接显示日期 (mm/dd/yy)
%h : 同 %b
%j : 一年中的第几天 (001-366)
%m : 月份 (01-12)
%U : 一年中的第几周 (00-53) (以 Sunday 为一周的第一天的情形)
%w : 一周中的第几天 (0-6)
% W: week number (00-53) (in the first day of the week Monday to case) the year
% x: direct display date (mm / dd / YY)
% Y: the last two digits of the year ( 00.99)
% the Y-: full year (0000-9999)

Do not have to remember all the above format, you only need to master a few common. E.g. expressed in% Y,% m is the month,% d represents day,% H represents hours,% M indicates min,% S represents seconds,% s represents from 1 January 1970 00:00:00 UTC date the number of seconds, the equivalent time function,% w represents the day of the week.

Examples below:

If you want to display the current time is not time, but the elapsed time operation, you can use the -d option. Three years ago, for example, display the time:

I was very hurt to see this command, the last time can not go back, only to see the command. Show time after three months:

Show time after ten days:

Sometimes the need to obtain the current time from at 0:00 on 1970 experienced the number of seconds 0 seconds, stored in a variable:

 

2, set the time

You can set the system time with the -s option:

A variety of ways, either you choose:

You have to believe in intelligent enough date of.

 

 


 

 

With more than a place:

Use  the date command  to get the current date, and to create a new folder based on this date.

 

Guess you like

Origin www.cnblogs.com/wzy23/p/11388978.html