标签:格式 javascrip 日期类 push nbsp 类型 cond rip second
/** * 日期格式化 * @param format * 格式化参数,支持各种日期格式 * @returns {string} * 返回格式化后日期 */ /* 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}|[\--\/-年-月-日-时-分-秒-\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() + 1 date.push(dNumber(month)) break case ‘M‘: 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 case ‘H‘: 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 } } return date.join(‘‘) }
javascript日期格式化的实现,支持n多种格式化日期类型
标签:格式 javascrip 日期类 push nbsp 类型 cond rip second
原文地址:http://www.cnblogs.com/chuangyidai/p/6822843.html