2019-04-21 11:50:48 +08:00

95 lines
5.0 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1 id="date">date</h1>
<p>显示或设置系统时间与日期</p>
<h2 id="补充说明">补充说明</h2>
<p><strong>date命令</strong> 是显示或设置系统时间与日期。</p>
<p>很多shell脚本里面需要打印不同格式的时间或日期以及要根据时间和日期执行操作。延时通常用于脚本执行过程中提供一段等待的时间。日期可以以多种格式去打印也可以使用命令设置固定的格式。在类UNIX系统中日期被存储为一个整数其大小为自世界标准时间UTC1970年1月1日0时0分0秒起流逝的秒数。</p>
<h3 id="语法">语法</h3>
<pre><code class="language-bash">date(选项)(参数)</code></pre>
<h3 id="选项">选项</h3>
<pre><code class="language-bash">-d&lt;字符串&gt;:显示字符串所指的日期与时间。字符串前后必须加上双引号;
-s&lt;字符串&gt;:根据字符串来设置日期与时间。字符串前后必须加上双引号;
-u显示GMT
--help在线帮助
--version显示版本信息。</code></pre>
<h3 id="参数">参数</h3>
<p>&lt;+时间日期格式&gt;:指定显示时使用的日期时间格式。</p>
<h3 id="日期格式字符串列表">日期格式字符串列表</h3>
<pre><code class="language-bash">%H 小时24小时制00~23
%I 小时12小时制01~12
%k 小时24小时制0~23
%l 小时12小时制1~12
%M 分0059
%p 显示出上午或下午
%r 时间12小时制
%s 从1970年1月1日0点到目前经历的秒数
%S 秒0059
%T 时间24小时制hh:mm:ss
%X 显示时间的格式H时M分S秒
%Z 按字母表排序的时区缩写
%a 星期名缩写
%A 星期名全称
%b 月名缩写
%B 月名全称
%c 日期和时间
%d 按月计的日期0131
%D 日期mm/dd/yy
%h 和%b选项相同
%j 一年的第几天001~366
%m 月份0112
%w 一个星期的第几天0代表星期天
%W 一年的第几个星期0053星期一为第一天
%x 显示日期的格式mm/dd/yy
%y 年份的最后两个数字1999则是99
%Y 年份比如1970、1996等
%C 世纪,通常为省略当前年份的后两位数字
%U 一年中的第几周,以周日为每星期第一天
%e 按月计的日期,添加空格,等于%_d</code></pre>
<h3 id="实例">实例</h3>
<p>格式化输出:</p>
<pre><code class="language-bash">date +&quot;%Y-%m-%d&quot;
2009-12-07</code></pre>
<p>输出昨天日期:</p>
<pre><code class="language-bash">date -d &quot;1 day ago&quot; +&quot;%Y-%m-%d&quot;
2012-11-19</code></pre>
<p>2秒后输出</p>
<pre><code class="language-bash">date -d &quot;2 second&quot; +&quot;%Y-%m-%d %H:%M.%S&quot;
2012-11-20 14:21.31</code></pre>
<p>传说中的 1234567890 秒:</p>
<pre><code class="language-bash">date -d &quot;1970-01-01 1234567890 seconds&quot; +&quot;%Y-%m-%d %H:%m:%S&quot;
2009-02-13 23:02:30</code></pre>
<p>普通转格式:</p>
<pre><code class="language-bash">date -d &quot;2009-12-12&quot; +&quot;%Y/%m/%d %H:%M.%S&quot;
2009/12/12 00:00.00</code></pre>
<p>apache格式转换</p>
<pre><code class="language-bash">date -d &quot;Dec 5, 2009 12:00:37 AM&quot; +&quot;%Y-%m-%d %H:%M.%S&quot;
2009-12-05 00:00.37</code></pre>
<p>格式转换后时间游走:</p>
<pre><code class="language-bash">date -d &quot;Dec 5, 2009 12:00:37 AM 2 year ago&quot; +&quot;%Y-%m-%d %H:%M.%S&quot;
2007-12-05 00:00.37</code></pre>
<p>加减操作:</p>
<pre><code class="language-bash">date +%Y%m%d //显示前天年月日
date -d &quot;+1 day&quot; +%Y%m%d //显示前一天的日期
date -d &quot;-1 day&quot; +%Y%m%d //显示后一天的日期
date -d &quot;-1 month&quot; +%Y%m%d //显示上一月的日期
date -d &quot;+1 month&quot; +%Y%m%d //显示下一月的日期
date -d &quot;-1 year&quot; +%Y%m%d //显示前一年的日期
date -d &quot;+1 year&quot; +%Y%m%d //显示下一年的日期</code></pre>
<p>设定时间:</p>
<pre><code class="language-bash">date -s //设置当前时间只有root权限才能设置其他只能查看
date -s 20120523 //设置成20120523这样会把具体时间设置成空00:00:00
date -s 01:01:01 //设置具体时间,不会对日期做更改
date -s &quot;01:01:01 2012-05-23&quot; //这样可以设置全部时间
date -s &quot;01:01:01 20120523&quot; //这样可以设置全部时间
date -s &quot;2012-05-23 01:01:01&quot; //这样可以设置全部时间
date -s &quot;20120523 01:01:01&quot; //这样可以设置全部时间</code></pre>
<p>有时需要检查一组命令花费的时间,举例:</p>
<pre><code class="language-bash">#!/bin/bash
start=$(date +%s)
nmap wangchujiang.com &amp;&gt; /dev/null
end=$(date +%s)
difference=$(( end - start ))
echo $difference seconds.</code></pre>
<!-- Linux命令行搜索引擎https://jaywcjlove.github.io/linux-command/ -->