JS中常用的时间方法有哪些

一个新手
Lepaskan: 2017-09-19 10:40:31
asal
1725 orang telah melayarinya

这篇文章主要是分享几个比较常用的时间方法:

  • 时间格式化

  • 获取前几天或后几天的日期

  • 获取某月有多少天

  • 获取星期几

  • 获取两个日期时间差

<code class="hljs coffeescript"></code><p><span style="color:#009a61"><span class="hljs-regexp">//</span>格式化日期</span>   </p><p>Date.prototype.Format = function(fmt) {   </p><p>  var o = {   </p><p>    <span class="hljs-string">"M+"</span>: <span class="hljs-keyword">this</span>.getMonth() + <span class="hljs-number">1</span>, <span class="hljs-regexp">//</span>月份      </p><p>    <span class="hljs-string">"d+"</span>: <span class="hljs-keyword">this</span>.getDate(), <span class="hljs-regexp">//</span>日      </p><p>    <span class="hljs-string">"h+"</span>: <span class="hljs-keyword">this</span>.getHours(), <span class="hljs-regexp">//</span>小时      </p><p>    <span class="hljs-string">"m+"</span>: <span class="hljs-keyword">this</span>.getMinutes(), <span class="hljs-regexp">//</span>分      </p><p>    <span class="hljs-string">"s+"</span>: <span class="hljs-keyword">this</span>.getSeconds(), <span class="hljs-regexp">//</span>秒      </p><p>    <span class="hljs-string">"q+"</span>: Math.floor((<span class="hljs-keyword">this</span>.getMonth() + <span class="hljs-number">3</span>) / <span class="hljs-number">3</span>), <span class="hljs-regexp">//</span>季度      </p><p>    <span class="hljs-string">"S"</span>: <span class="hljs-keyword">this</span>.getMilliseconds() <span class="hljs-regexp">//</span>毫秒      </p><p>  };   </p><p>  <span class="hljs-keyword">if</span>(<span class="hljs-regexp">/(y+)/</span>.test(fmt)) {   </p><p>    fmt = fmt.replace(RegExp.$<span class="hljs-number">1</span>, (<span class="hljs-keyword">this</span>.getFullYear() + <span class="hljs-string">""</span>).substr(<span class="hljs-number">4</span> - RegExp.$<span class="hljs-number">1.</span>length));   </p><p>  };   </p><p>  <span class="hljs-keyword">for</span>(var k <span class="hljs-keyword">in</span> o) {   </p><p>    <span class="hljs-keyword">if</span>(<span class="hljs-keyword">new</span> RegExp(<span class="hljs-string">"("</span> + k + <span class="hljs-string">")"</span>).test(fmt)) {   </p><p>       fmt = fmt.replace(RegExp.$<span class="hljs-number">1</span>, (RegExp.$<span class="hljs-number">1.</span>length == <span class="hljs-number">1</span>) ? (o[k]) : ((<span class="hljs-string">"00"</span> + o[k]).substr((<span class="hljs-string">""</span> + o[k]).length)));   </p><p>    }   </p><p>  };   </p><p>   <span class="hljs-keyword">return</span> fmt; </p><p>};   </p><p><span style="color:#009a61"><span class="hljs-regexp">//</span>获取前几天或后几天的日期;正数表示前d天,负数表示后d天</span>   </p><p>Date.prototype.getWitchDate=function(d){   </p><p>  <span class="hljs-keyword">if</span>(<span class="hljs-regexp">/(\d)/</span>.test(d)){   </p><p>    d=<span class="hljs-keyword">this</span>.getDate()+d;   </p><p>    <span class="hljs-keyword">this</span>.setDate(d)   </p><p>  };   </p><p>  <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;   </p><p>};   </p><p><span style="color:#009a61"><span class="hljs-regexp">//</span>获取某月有多少天</span>   </p><p>Date.prototype.getMonthTotalDay=function(){   </p><p>  <span class="hljs-keyword">this</span>.setDate(<span class="hljs-number">32</span>);   </p><p>  <span class="hljs-keyword">return</span> <span class="hljs-number">32</span>-<span class="hljs-keyword">this</span>.getDate();   </p><p>};   </p><p><span style="color:#009a61"><span class="hljs-regexp">//</span>获取周几   </span></p><p>Date.prototype.getWeek=function(d){   </p><p>  var week=[<span class="hljs-string">&#39;星期一&#39;</span>,<span class="hljs-string">&#39;星期二&#39;</span>,<span class="hljs-string">&#39;星期三&#39;</span>,<span class="hljs-string">&#39;星期四&#39;</span>,<span class="hljs-string">&#39;星期五&#39;</span>,<span class="hljs-string">&#39;星期六&#39;</span>,<span class="hljs-string">&#39;星期天&#39;</span>];   </p><p>  <span class="hljs-keyword">if</span>(<span class="hljs-keyword">typeof</span> d !== <span class="hljs-string">&#39;undefined&#39;</span>){   </p><p>    <span class="hljs-keyword">return</span> week[parseInt(d)%<span class="hljs-number">7</span>];   </p><p>  };   </p><p>  <span class="hljs-keyword">return</span> week[<span class="hljs-keyword">this</span>.getDay()];
            };<br/></p><p><span style="color:#009a61"><span class="hljs-regexp">//</span>获取两个日期的时间差,单位秒</span>   </p><p>Date.prototype.getDateSecond=function(d){   </p><p>  <span class="hljs-keyword">if</span>(<span class="hljs-keyword">typeof</span> d === <span class="hljs-string">&#39;string&#39;</span>){   </p><p>    d=<span class="hljs-keyword">new</span> Date(d);   </p><p>  }   </p><p>  var t1=<span class="hljs-keyword">this</span>.getTime();   </p><p>  var t2=d.getTime();   </p><p>  <span class="hljs-keyword">return</span> Math.floor(Math.abs(t1-t2)/<span class="hljs-number">1000</span>);   </p><p>};<br/></p>
Salin selepas log masuk

用法:

<code class="hljs javascript"></code><p><span class="hljs-keyword">var</span> h = <span class="hljs-string">&#39;当前时间是&#39;</span> + d.Format(<span class="hljs-string">&#39;yyyy-MM-dd hh:mm:ss&#39;</span>)+<span class="hljs-string">&#39;</br>&#39;</span>;<br/></p><p>h += <span class="hljs-string">&#39;3天前是&#39;</span> + d.getWitchDate(<span class="hljs-number">-3</span>).Format(<span class="hljs-string">&#39;yyyy-MM-dd hh:mm:ss&#39;</span>)+<span class="hljs-string">&#39;</br>&#39;</span>;   </p><p>h += <span class="hljs-string">&#39;9月份有&#39;</span> + d.getMonthTotalDay()+<span class="hljs-string">&#39;天</br>&#39;</span>;   </p><p>h += <span class="hljs-string">&#39;今天是&#39;</span> + d.getWeek()+<span class="hljs-string">&#39;</br>&#39;</span>; </p><p>h += <span class="hljs-string">&#39;今天距离2016年9月15日相差&#39;</span> +  d.getDateSecond(<span class="hljs-string">&#39;2016/9/15&#39;</span>) +<span class="hljs-string">&#39;秒&#39;</span></p>
Salin selepas log masuk

Atas ialah kandungan terperinci JS中常用的时间方法有哪些. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!