Home > Web Front-end > JS Tutorial > body text

What are the commonly used time methods in JS?

一个新手
Release: 2017-09-19 10:40:31
Original
1725 people have browsed it

This article mainly shares several commonly used time methods:

  • Time formatting

  • Get the days before or after The date of several days

  • Get how many days there are in a certain month

  • Get the day of the week

  • Get Difference between two dates and times

<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>
Copy after login

Usage:

<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>
Copy after login

The above is the detailed content of What are the commonly used time methods in JS?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!