/** * Date format * @param format * Format parameter, support various date formats * @returns {string} * return formatted date*//*eslint-disable no-extend-native*//*eslint-disable Indent*/Date.prototype.dateFormart= Date.prototype.dateFormart | |function(format) {Console.log ( This) /*eslint-disable No-useless-escape*/Let formatString= Format.match (/[a-za-z]{1,4}|[ \--\/-year-month-day-time-minute-second-\s-:]/g) Let date= [] for(Let i = 0, len = formatstring.length; i < Len; i++) { Switch(Formatstring[i]) { Case' YYYY ': Date.push ( This. getFullYear ()) Break Case' YY ': Date.push ( This. GetYear ()) Break Case' MM ': Let month= This. GetMonth () + 1Date.push (Dnumber (month)) Break CaseM: Date.push ( This. GetMonth () + 1) Break Case' DD ': Date.push (Dnumber ( This. GetDate ())) Break Case' d ': Date.push ( This. GetDate ()) Break Case' HH ': Date.push (Dnumber ( This. GetHours ())) Break CaseH: Date.push ( This. GetHours ()) Break Case' mm ': Date.push (Dnumber ( This. getminutes ())) Break Case' m ': Date.push ( This. getminutes ()) Break Case' SS ': Date.push (Dnumber ( This. getseconds ())) Break Case' s ': Date.push ( This. getseconds ()) Break default: Date.push (Formatstring[i]) Break } } returnDate.join (' ') }
JavaScript implementation of date formatting, supporting n multiple formatted date types