Javascript Date formatting related actions

Source: Internet
Author: User

1. Related extension functions
---------------------------------------------------//judgment Leap year//-------------------------------------------------- -date.prototype.isleapyear = function () {return (0==this.getyear ()%4&& ((this.getyear ()%100!=0) | | (this.getyear ()%400==0));};/ /---------------------------------------------------//date Formatting//format Yyyy/yyyy/yy/yy represents the year//mm/m month//w/w week//dd/dd/d/d date hh/hh/h/h time//mm/m min//ss/ss/s/s Sec//---------------------------------------------------Date.prototype.Format = function (formatstr) {var str = Formatstr;var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six '];str=str.replace (/yyyy| Yyyy/,this.getfullyear ()); Str=str.replace (/yy| yy/, (this.getyear ()%) >9? (this.getyear ()%). toString (): ' 0 ' + (this.getyear ()%)); str=str.replace (/mm/, (this.getmonth () + 1) >9? ( This.getmonth () + 1). toString (): ' 0 ' + (this.getmonth () + 1)); str=str.replace (/m/g,this.getmonth () + 1); Str=str.replace (/w| W/g,week[this.getday ()]); Str=str.replace (/dd| Dd/,this.getdate () >9?this.getdate (). toString (): ' 0 ' + thiS.getdate ()); Str=str.replace (/d| D/g,this.getdate ()); Str=str.replace (/hh| Hh/,this.gethours () >9?this.gethours (). toString (): ' 0 ' + this.gethours ()); str=str.replace (/h| H/g,this.gethours ()); str=str.replace (/mm/,this.getminutes () >9?this.getminutes (). toString (): ' 0 ' + This.getminutes ()); str=str.replace (/m/g,this.getminutes ()); str=str.replace (/ss| Ss/,this.getseconds () >9?this.getseconds (). toString (): ' 0 ' + this.getseconds ()); str=str.replace (/s| S/g,this.getseconds ()); return str;};/ /+---------------------------------------------------//| Two days Difference date format is yyyy-mm-dd//+---------------------------------------------------function daysbetween (dateone, Datetwo) {var onemonth = dateone.substring (5,dateone.lastindexof ('-')); var oneday = dateone.substring (DateOne.length, Dateone.lastindexof ('-') +1); var oneyear = dateone.substring (0,dateone.indexof ('-')); var twomonth = datetwo.substring (5,datetwo.lastindexof ('-')); var twoday = datetwo.substring (datetwo.length,datetwo.lastindexof ('-') +1); var TwoYear = DatEtwo.substring (0,datetwo.indexof ('-')), var cha= (date.parse (onemonth+ '/' +oneday+ '/' +oneyear)-date.parse ( twomonth+ '/' +twoday+ '/' +twoyear)/86400000); return math.abs (cha);} //+---------------------------------------------------//| Date Calculation//+---------------------------------------------------Date.prototype.DateAdd = function (strinterval, Number) { var dttmp = This;switch (strinterval) {case ' s ': return new Date (date.parse (dttmp.togmtstring ()) + (* number)); n ': return new date (date.parse (dttmp.togmtstring ()) + (60000 * number)), case ' H ': return new date (date.parse (dttmp.togmts Tring ()) + (3600000 * number), case ' d ': return new Date (date.parse (dttmp.togmtstring ()) + (86400000 * number), case ' W ': return new Date (date.parse (dttmp.togmtstring ()) + ((86400000 * 7) * number))//fixme The following two kinds of increase in the cross-year is Abnormal//case ' Q ': return new D Ate (dttmp.getfullyear (), (dttmp.getmonth ()) + number*3, dttmp.getdate (), dttmp.gethours (), dttmp.getminutes (), Dttmp.getseconds ());//case ' m ': return new Date (dttmp.getfuLlyear (), (dttmp.getmonth ()) + number, dttmp.getdate (), dttmp.gethours (), dttmp.getminutes (), dttmp.getseconds ()); Case ' Y ': return new Date ((dttmp.getfullyear () + number), dttmp.getmonth (), dttmp.getdate (), dttmp.gethours (), Dttmp.getminutes (), dttmp.getseconds ());}};/ /+---------------------------------------------------//| Comparison Date Difference dtend format is a date type or valid date format String//+---------------------------------------------------Date.prototype.DateDiff = function (strinterval, dtend) {var dtstart = this;if (typeof dtend = = ' String ')//if the string is converted to a date type {dtend = Stringtodate (dtend); }switch (strinterval) {case ' s ': return parseint ((dtend-dtstart)/+); case ' n ': return parseint ((DTEND-DTSTART)/6 0000); case ' H ': return parseint ((dtend-dtstart)/3600000), case ' d ': return parseint ((dtend-dtstart)/86400000), case ' W ': return parseint ((dtend-dtstart)/(86400000 * 7)), case ' m ': return (dtend.getmonth () +1) + ((dtend.getfullyear ()-dtst Art.getfullyear ()) *12)-(dtstart.getmonth () +1); case ' y ': return dtend.getfulLyear ()-dtstart.getfullyear ();}};/ /+---------------------------------------------------//| A date output string that overloads the System's toString method//+---------------------------------------------------Date.prototype.toString = function (showweek) {var mydate= This;var str = mydate.tolocaledatestring (); if (showweek) {var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six '];str + = ' Week ' + Week[mydate.getday ()];} Return str;};

2. Call format
Formatted date var date=new date (); var res=date. Format ("yyyy-mm-dd HH:mm:ss  week w"), Console.info (res), var date1=new  Date (),//add 2 weeks var date2=date1. DateAdd (' W ', 2);//find the date difference var dif=daysbetween (date1. Format ("yyyy-mm-dd"), date2. Format ("yyyy-mm-dd")) console.info (dif);//compare date difference var datediff=date1. DateDiff (' d ', date2); console.info (DateDiff);//tostringconsole.info (date1.tostring ())//determine If it is a leap year console.info ( Date1.isleapyear ())

3. Results
2014-10-08 10:13:25  Wednesday 14132014/10/8false

Original: http://blog.csdn.net/whzhaochao/article/details/39890321

Javascript Date formatting related actions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.