/** * Extension to Date, String * months (m), days (d), hours (h), minutes (m), seconds (s), quarter (q) can be used with 1-2 placeholders, * year (y) can be used with 1-4 placeholders, and milliseconds (s) may only be used with 1 placeholders (1 -3 digits) * Example: * (new Date ()). Format ("Yyyy-mm-dd hh:mm:ss. S ") ==> 2006-07-02 08:09:04.423 * (New Date ()). Format ("yyyy-m-d h:m:s.s") ==> 2006-7-2 8:9:4.18*/Date.prototype.format=function(FMT) {varo = { ' m+ ': This. GetMonth () + 1,//Month' D+ ': This. GetDate (),//Day' H + ': This. GetHours (),//hours' H + ': This. GetHours (),//hours' m+ ': This. getminutes (),//points' S+ ': This. getseconds (),//seconds' q+ ': Math.floor (( This. GetMonth () + 3)/3),//Quarterly' S ': This. Getmilliseconds ()//milliseconds } if(/(y+)/.test (FMT)) FMT = Fmt.replace (regexp.$1, ( This. getFullYear () + "). substr (4-regexp.$1. Length)) for(varKincho)if(NewRegExp (' (' + K + ') '). Test (FMT)) FMT = Fmt.replace (regexp.$1, (regexp.$1.length = = 1)? (O[k]): ((' xx ' + o[k]). substr (' + ' +O[k] )))returnFMT}
/** *js Change date * y year, M-month, D-Day, H-hour, N-minute, S-second * var start = new Date (), *start.add ("D",-1);//Yesterday *start.format (' Yyyy/mm/dd w ') ; Format *start.add ("M",-1); Last month*/Date.prototype.add=function(part, value) {Value*= 1; if(IsNaN (value)) {Value= 0; } Switch(part) { Case"Y": This. setFullYear ( This. getFullYear () +value); Break; Case"M": This. Setmonth ( This. GetMonth () +value); Break; Case"D": This. SetDate ( This. GetDate () +value); Break; Case"H": This. sethours ( This. getHours () +value); Break; CaseN: This. Setminutes ( This. getminutes () +value); Break; Case"S": This. Setseconds ( This. getseconds () +value); Break; default: } return This;}
If this article is helpful to you, you can give me a reward.
Technical Exchange QQ Group: 15129679
JavaScript extended time method, format, add minus date