//demo:new Date (). Format ("Yyyy-mm-dd Hh:mm:ss. SSS ")Date.prototype.format =function(format) {varo = { "m+": This. GetMonth () + 1,//Month"D+": This. GetDate (),// Day"H +": This. GetHours (),//Hour"m+": This. getminutes (),//minute"S+": This. getseconds (),//Second"q+": Math.floor (( This. GetMonth () + 3)/3),//Quarter"S": This. Getmilliseconds ()//Millisecond } if(/(y+)/.test (format)) format = Format.replace (regexp.$1, ( This. getFullYear () + ""). substr (4-regexp.$1. length)); for(varKinchOif(NewRegExp ("(" + K + ")"). Test (format)) format= Format.replace (regexp.$1, regexp.$1.length = = 1?O[k]: ("XX" + o[k]). substr ("" +O[k]) (length)); returnformat;} Date.prototype.addYears=function(num) {num=parseint (num); varSyear = This. getFullYear (); varSmonth = This. GetMonth (); varSday = This. GetDate (); varEyear = Syear +num; varEmonth =Smonth; varEday =Sday; varEDate =NewDate (Eyear, Emonth, Eday); while(Edate.getmonth ()! =emonth) {Eday--; EDate=NewDate (Eyear, Emonth, Eday); } varTimes = Edate.gettime ()-NewDate (Syear, Smonth, Sday). GetTime (); return NewDate (NewDate (). GetTime () +Times );}//demo:new Date (). AddMonths ( -1)Date.prototype.addMonths =function(num) {num=parseint (num); varSyear = This. getFullYear (); varSmonth = This. GetMonth () + 1; varSday = This. GetDate (); varEyear =syear; varEmonth = Smonth +num; varEday =Sday; while(Emonth > 12) {eyear++; Emonth-= 12; } while(Emonth <= 0) {eyear--; Emonth+ = 12; } varEDate =NewDate (Eyear, eMonth-1, Eday); while(Edate.getmonth ()! = EMonth-1) {Eday--; EDate=NewDate (Eyear, eMonth-1, Eday); } varTimes = Edate.gettime ()-NewDate (Syear, sMonth-1, Sday). GetTime (); return NewDate (NewDate (). GetTime () +Times );} Date.prototype.addDates=function(num) {return This. addHours (NUM * 24);} Date.prototype.addHours=function(num) {return This. addminutes (NUM * 60);} Date.prototype.addMinutes=function(num) {return This. addseconds (NUM * 60);} Date.prototype.addSeconds=function(num) {num=parseint (num); return NewDate (NewDate (). GetTime () + num * 1000);}
JS date formatting and date increment