Heim  >  Artikel  >  Web-Frontend  >  Was sind die am häufigsten verwendeten Zeitmethoden in JS?

Was sind die am häufigsten verwendeten Zeitmethoden in JS?

一个新手
一个新手Original
2017-09-19 10:40:311723Durchsuche

In diesem Artikel werden hauptsächlich einige häufig verwendete Zeitmethoden beschrieben:

  • Zeitformatierung

  • Erhalten Sie die Tage vor oder nach dem Datum mehrerer Tage

  • Ermitteln Sie die Anzahl der Tage in einem Monat

  • Erhalten Sie den Wochentag

  • Differenz zwischen zwei Datums- und Uhrzeitangaben abrufen

<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>

Verwendung:

<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>

Das obige ist der detaillierte Inhalt vonWas sind die am häufigsten verwendeten Zeitmethoden in JS?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn