Home  >  Article  >  Web Front-end  >  js acquisition date: yesterday, today, tomorrow, the day after tomorrow_javascript skills

js acquisition date: yesterday, today, tomorrow, the day after tomorrow_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:44:531196browse
Copy code The code is as follows:



< ;meta http-equiv="Content-Type" content="textml; charset=utf-8">
js acquisition date: the day before yesterday, yesterday, today, tomorrow, the day after tomorrow - Liehuo.Net</ title> <br></head> <br><br><body> <br><script language="JavaScript" type="text/javascript"> <br>function GetDateStr(AddDayCount) { <br>var dd = new Date(); <br>dd.setDate(dd.getDate() AddDayCount);//Get the date after AddDayCount days <br>var y = dd.getFullYear(); <br>var m = dd.getMonth() 1;//Get the date of the current month <br>var d = dd.getDate(); <br>return y "-" m "-" d; <br>} <br>document.write ("The day before yesterday:" GetDateStr(-2)); <br>document.write("<br />Yesterday:" GetDateStr(-1)); <br>document.write("<br /> Today: " GetDateStr(0)); <br>document.write("<br />Tomorrow: " GetDateStr(1)); <br>document.write("<br />The day after tomorrow: " GetDateStr (2)); <br>document.write("<br />Day after tomorrow:" GetDateStr(3)); <br></script> <br><br></body> <br> <ml> <br> </div> <br>One of the methods is: Date.parse(dateVal). This function is powerful, but it has a fatal shortcoming, that is, it does not support our commonly used "year-month- "Day" format, short dates can use "/" or "-" as the date separator, but must be expressed in month/day/year format, such as "7/20/96". <br><br>Another method is to use split, such as: <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="43752" class="copybut" id="copybut43752" onclick="doCopy('code43752')"><u>Copy the code</u></a></span> The code is as follows:</div> <div class="codebody" id="code43752"> <br>var dtStr = "2006-11-25"; <br>var dtArr = dtStr.split("-"); <br>var dt = new Date(dtArr[0], dtArr[1], dtArr [2]); <br> </div> <br>However, this method is relatively rigid and requires a fixed date format. It should only be used when there is no other way. <br><br>If we can separate the year, month and day, we should try to separate them. For example, ASP outputs the year, month and day separately. Then use new Date to process it, and the date type will be returned. <br><br>Date formatting<br><div class="codetitle"> <span><a style="CURSOR: pointer" data="21725" class="copybut" id="copybut21725" onclick="doCopy('code21725')"><u>Copy code</u></a></span> The code is as follows:</div> <div class="codebody" id="code21725"> <br><script language="javascript" type="text/javascript"><!-- <BR>/**<BR>* Extension of Date, convert Date into String in specified format <BR>* Month (M), day (d), 12 hours (h), 24 hours (H), minutes (m), seconds ( s), week (E), quarter (q) can use 1-2 placeholders <BR>* year (y) can use 1-4 placeholders, milliseconds (S) can only use 1 placeholder symbol (is a number of 1-3 digits) <BR>* eg: <BR>* (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2006- 07-02 08:09:04.423 <br>* (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 Tuesday 20:09:04 <br>* (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04 <br>* (new Date( )).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04 <br>* (new Date()).pattern("yyyy- M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 <br>*/ <br>Date.prototype.pattern=function(fmt) { <br>var o = { <br>"M " : this.getMonth() 1, //Month<br>"d " : this.getDate(), //Day<br>"h " : this.getHours() == 0 ? 12: this.getHours(), //Hours<br>"H ": this.getHours(), //Hours<br>"m " : this.getMinutes(), //Minutes<br>"s " : this.getSeconds(), //Seconds<br>"q " : Math.floor((this.getMonth() 3)/3), //Quarter<br>"S" : this.getMilliseconds() / /ms<br>}; <br>var week = { <br>"0" : "u65e5", <br>"1" : "u4e00", <br>"2" : "u4e8c", <br> "3" : "u4e09", <br>"4" : "u56db", <br>"5" : "u4e94", <br>"6" : "u516d" <br>}; <br>if( /(y )/.test(fmt)){ <br>fmt=fmt.replace(RegExp.$1, (this.getFullYear() "").substr(4 - RegExp.$1.length)); <br> } <br>if(/(E )/.test(fmt)){ <br>fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "u661fu671f" : "u5468") : "") week[this.getDay() ""]); <br>} <br>for(var k in o){ <br>if(new RegExp("( " k ")").test(fmt)){ <br>fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00" o [k]).substr(("" o[k]).length))); <br>} <br>} <br>return fmt; <br>} <br><br>var date = new Date (); <br>window.alert(date.pattern("yyyy-MM-dd hh:mm:ss")); <br>// --></script> <br> </div></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>Statement:</span><div>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</div></div></div><div class="nphpSytBox"><span>Previous article:<a class="dBlack" title="How to make the browser support jquery ajax load forward and backward functions_jquery" href="http://m.php.cn/faq/12544.html">How to make the browser support jquery ajax load forward and backward functions_jquery</a></span><span>Next article:<a class="dBlack" title="How to make the browser support jquery ajax load forward and backward functions_jquery" href="http://m.php.cn/faq/12546.html">How to make the browser support jquery ajax load forward and backward functions_jquery</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>Related articles</h2><em><a href="http://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ul class="nphpXgwzList"><li><b></b><a href="http://m.php.cn/faq/1609.html" title="An in-depth analysis of the Bootstrap list group component" class="aBlack">An in-depth analysis of the Bootstrap list group component</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/1640.html" title="Detailed explanation of JavaScript function currying" class="aBlack">Detailed explanation of JavaScript function currying</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/1949.html" title="Complete example of JS password generation and strength detection (with demo source code download)" class="aBlack">Complete example of JS password generation and strength detection (with demo source code download)</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/2248.html" title="Angularjs integrates WeChat UI (weui)" class="aBlack">Angularjs integrates WeChat UI (weui)</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/2351.html" title="How to quickly switch between Traditional Chinese and Simplified Chinese with JavaScript and the trick for websites to support switching between Simplified and Traditional Chinese_javascript skills" class="aBlack">How to quickly switch between Traditional Chinese and Simplified Chinese with JavaScript and the trick for websites to support switching between Simplified and Traditional Chinese_javascript skills</a><div class="clear"></div></li></ul></div></div><footer><div class="footer"><div class="footertop"><img src="/static/imghwm/logo.png" alt=""><p>Public welfare online PHP training,Help PHP learners grow quickly!</p></div><div class="footermid"><a href="http://m.php.cn/about/us.html">About us</a><a href="http://m.php.cn/about/disclaimer.html">Disclaimer</a><a href="http://m.php.cn/update/article_0_1.html">Sitemap</a></div><div class="footerbottom"><p> © php.cn All rights reserved </p></div></div></footer><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body></html>