Heim > Artikel > Web-Frontend > Was sind die am häufigsten verwendeten Zeitmethoden in JS?
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">'星期一'</span>,<span class="hljs-string">'星期二'</span>,<span class="hljs-string">'星期三'</span>,<span class="hljs-string">'星期四'</span>,<span class="hljs-string">'星期五'</span>,<span class="hljs-string">'星期六'</span>,<span class="hljs-string">'星期天'</span>]; </p><p> <span class="hljs-keyword">if</span>(<span class="hljs-keyword">typeof</span> d !== <span class="hljs-string">'undefined'</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">'string'</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">'当前时间是'</span> + d.Format(<span class="hljs-string">'yyyy-MM-dd hh:mm:ss'</span>)+<span class="hljs-string">'</br>'</span>;<br/></p><p>h += <span class="hljs-string">'3天前是'</span> + d.getWitchDate(<span class="hljs-number">-3</span>).Format(<span class="hljs-string">'yyyy-MM-dd hh:mm:ss'</span>)+<span class="hljs-string">'</br>'</span>; </p><p>h += <span class="hljs-string">'9月份有'</span> + d.getMonthTotalDay()+<span class="hljs-string">'天</br>'</span>; </p><p>h += <span class="hljs-string">'今天是'</span> + d.getWeek()+<span class="hljs-string">'</br>'</span>; </p><p>h += <span class="hljs-string">'今天距离2016年9月15日相差'</span> + d.getDateSecond(<span class="hljs-string">'2016/9/15'</span>) +<span class="hljs-string">'秒'</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!